diff --git a/src/index.ts b/src/index.ts index d647f50..a926749 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import type grapesjs from 'grapesjs'; -import loadComponents from './components'; +// import loadComponents from './components'; export type PluginOptions = { /** @@ -112,10 +112,11 @@ const plugin: grapesjs.Plugin = (editor, opts = {}) => { const { block } = options; const id = options.id!; const label = options.label!; + const pfx = options.classPrefix!; // Create block if (block) { - editor.BlockManager.add(id, { + editor.Blocks.add(id, { media: ` `, @@ -125,7 +126,134 @@ const plugin: grapesjs.Plugin = (editor, opts = {}) => { content: { type: id }, ...block }); - } + }; + + const coundownScript = function(props: any) { + const startfrom: string = props.startfrom; + const endTxt: string = props.endText; + // @ts-ignore + const el: HTMLElement & { gjs_countdown_interval: NodeJS.Timer } = this; + const countDownDate = new Date(startfrom).getTime(); + const countdownEl = el.querySelector('[data-js=countdown]') as HTMLElement; + const endTextEl = el.querySelector('[data-js=countdown-endtext]') as HTMLElement; + const dayEl = el.querySelector('[data-js=countdown-day]')!; + const hourEl = el.querySelector('[data-js=countdown-hour]')!; + const minuteEl = el.querySelector('[data-js=countdown-minute]')!; + const secondEl = el.querySelector('[data-js=countdown-second]')!; + const oldInterval = el.gjs_countdown_interval; + oldInterval && clearInterval(oldInterval); + + const setTimer = function (days: number, hours: number, minutes: number, seconds: number) { + dayEl.innerHTML = `${days < 10 ? '0' + days : days}`; + hourEl.innerHTML = `${hours < 10 ? '0' + hours : hours}`; + minuteEl.innerHTML = `${minutes < 10 ? '0' + minutes : minutes}`; + secondEl.innerHTML = `${seconds < 10 ? '0' + seconds : seconds}`; + } + + const moveTimer = function() { + const now = new Date().getTime(); + const distance = countDownDate - now; + const days = Math.floor(distance / 86400000); + const hours = Math.floor((distance % 86400000) / 3600000); + const minutes = Math.floor((distance % 3600000) / 60000); + const seconds = Math.floor((distance % 60000) / 1000); + + setTimer(days, hours, minutes, seconds); + + if (distance < 0) { + clearInterval(el.gjs_countdown_interval); + endTextEl.innerHTML = endTxt; + countdownEl.style.display = 'none'; + endTextEl.style.display = ''; + } + }; + + if (countDownDate) { + el.gjs_countdown_interval = setInterval(moveTimer, 1000); + endTextEl.style.display = 'none'; + countdownEl.style.display = ''; + moveTimer(); + } else { + setTimer(0, 0, 0, 0); + } + }; + + // Create component + editor.Components.addType(id, { + model: { + defaults: { + startfrom: options.startTime, + endText: options.endText, + droppable: false, + script: coundownScript, + 'script-props': ['startfrom', 'endText'], + traits: [{ + label: 'Start', + name: 'startfrom', + changeProp: true, + type: options.dateInputType, + },{ + label: 'End text', + name: 'endText', + changeProp: true, + }], + components: ` + +
+
+
${options.labelDays}
+
+
+
+
${options.labelHours}
+
+
+
+
${options.labelMinutes}
+
+
+
+
${options.labelSeconds}
+
+
+ + `, + }, + }, + view: { + init() { + // this.listenTo(this.model, 'change:startfrom change:endText', this.updateScript); + const comps = this.model.get('components'); + + // // Add a basic countdown template if it's not yet initialized + // if (comps && !comps?.length) { + // comps.reset(); + // comps.add(` + // + //
+ //
+ //
${c.labelDays}
+ //
+ //
+ //
+ //
${c.labelHours}
+ //
+ //
+ //
+ //
${c.labelMinutes}
+ //
+ //
+ //
+ //
${c.labelSeconds}
+ //
+ //
+ // + // `); + // } + + } + }, + }); /** const pfx = c.countdownClsPfx; @@ -157,7 +285,7 @@ const pfx = c.countdownClsPfx; */ // Add components - loadComponents(editor, options); + // loadComponents(editor, options); }; export default plugin; \ No newline at end of file