From 84c43d5baf0a7b336a38b5962f0e2dc57d079efd Mon Sep 17 00:00:00 2001 From: nevermoregb Date: Mon, 24 Feb 2025 15:27:45 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=B5=ED=86=B5=20=EB=82=A0=EC=A7=9C=20?= =?UTF-8?q?=ED=8F=AC=EB=A9=A7=ED=8C=85=20=ED=95=A8=EC=88=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/common.js | 90 ++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/src/common/common.js b/src/common/common.js index e761219..5436651 100644 --- a/src/common/common.js +++ b/src/common/common.js @@ -15,35 +15,69 @@ import Quill from 'quill'; $common.변수 */ const common = { - // JSON 문자열로 Delta 타입을 변환 - contentToHtml(content) { - try { - if (content.startsWith('{') || content.startsWith('[')) { - // Delta 형식으로 변환 - const delta = JSON.parse(content); - const quill = new Quill(document.createElement('div')); - quill.setContents(delta); - return quill.root.innerHTML; // HTML 반환 - } - return content; // 이미 HTML일 경우 그대로 반환 - } catch (error) { - console.error('콘텐츠 변환 오류:', error); - return content; // 오류 발생 시 원본 반환 - } - }, - // Delta 타입을 JSON 문자열로 변환 - deltaAsJson(content) { - if (content && content.ops) { - return JSON.stringify(content.ops); // Delta 객체에서 ops 속성만 JSON 문자열로 변환 - } - console.error('잘못된 Delta 객체:', content); - return null; // Delta 객체가 아니거나 ops가 없을 경우 null 반환 - } + // JSON 문자열로 Delta 타입을 변환 + contentToHtml(content) { + try { + if (content.startsWith('{') || content.startsWith('[')) { + // Delta 형식으로 변환 + const delta = JSON.parse(content); + const quill = new Quill(document.createElement('div')); + quill.setContents(delta); + return quill.root.innerHTML; // HTML 반환 + } + return content; // 이미 HTML일 경우 그대로 반환 + } catch (error) { + console.error('콘텐츠 변환 오류:', error); + return content; // 오류 발생 시 원본 반환 + } + }, + // Delta 타입을 JSON 문자열로 변환 + deltaAsJson(content) { + if (content && content.ops) { + return JSON.stringify(content.ops); // Delta 객체에서 ops 속성만 JSON 문자열로 변환 + } + console.error('잘못된 Delta 객체:', content); + return null; // Delta 객체가 아니거나 ops가 없을 경우 null 반환 + }, -} + /** + * Date 타입 문자열 포멧팅 + * + * @param {string} dateStr + * @return + * 1. Date type 인 경우 예시 '25-02-24 12:02' + * 2. Date type 이 아닌 경우 입력값 리턴 + * + */ + dateFormatter(dateStr) { + const date = new Date(dateStr); + const dateCheck = date.getTime(); + + if (isNaN(dateCheck)) { + return dateStr; + } else { + const { year, month, day, hours, minutes } = this.formatDateTime(date); + return `${year}-${month}-${day} ${hours}:${minutes}`; + } + }, + + formatDateTime(date) { + const zeroFormat = num => (num < 10 ? `0${num}` : num); + + return { + year: date.getFullYear(), + month: zeroFormat(date.getMonth() + 1), + day: zeroFormat(date.getDate()), + hours: zeroFormat(date.getHours()), + minutes: zeroFormat(date.getMinutes()), + seconds: zeroFormat(date.getSeconds()), + }; + }, +}; export default { - install(app) { - app.config.globalProperties.$common = common; - } + install(app) { + app.config.globalProperties.$common = common; + app.provide('common', common); + }, };