on-codemerge
Version:
A WYSIWYG editor for on-codemerge is a user-friendly interface that allows users to edit and view their code in real time, exactly as it will appear in the final product
3 lines (2 loc) • 6.09 kB
JavaScript
/*! on-codemerge v1.3.1 @author Pavel Kuzmin @license MIT @homepage https://s00d.github.io/on-codemerge/ @repository git+https://github.com/s00d/on-codemerge.git Copyright (c) 2026 Pavel Kuzmin - Built on 2026-07-02T13:39:17.947Z */
"use strict";var g=Object.defineProperty;var v=(d,e,t)=>e in d?g(d,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):d[e]=t;var l=(d,e,t)=>v(d,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});;/* empty css */;/* empty css */const b=require("./components/CalendarMenu.cjs"),E=require("./services/CalendarManager.cjs"),w=require("../ToolbarPlugin/utils.cjs"),y=require("../../icons/calendar.svg.cjs"),M=require("./components/CalendarContextMenu.cjs"),k=require("../../utils/helpers.cjs");class x{constructor(){l(this,"name","calendar");l(this,"hotkeys",[{keys:"Ctrl+Alt+L",description:"Insert calendar",command:"calendar",icon:"📅"}]);l(this,"editor",null);l(this,"menu",null);l(this,"manager");l(this,"toolbarButton",null);l(this,"contextMenu",null);this.manager=new E.CalendarManager}initialize(e){this.menu=new b.CalendarMenu(this.manager,e,()=>this.showImportDialog()),this.editor=e,this.contextMenu=new M.CalendarContextMenu(e,this.handleContextMenuAction.bind(this)),this.addToolbarButton(),this.setupEventListeners(),this.editor.on("calendar",()=>{this.showCalendarMenu()})}addToolbarButton(){var t,n;const e=(t=this.editor)==null?void 0:t.getToolbar();e&&(this.toolbarButton=w.createToolbarButton({icon:y.default,title:(n=this.editor)==null?void 0:n.t("Calendar"),onClick:()=>this.showCalendarMenu()}),e.appendChild(this.toolbarButton))}setupEventListeners(){if(!this.editor)return;const e=this.editor.getContainer();e.addEventListener("click",this.handleCalendarClick.bind(this)),e.addEventListener("contextmenu",this.handleContextMenu.bind(this))}handleCalendarClick(e){var r;const t=e.target.closest(".calendar-event");if(!t)return;e.preventDefault();const n=t.getAttribute("data-event-id");if(!n)return;const a=this.manager.getEvent(n);a&&((r=this.menu)==null||r.showEditEvent(a,()=>this.refreshCalendar()))}handleContextMenu(e){var a,r;const t=e.target.closest(".calendar-widget"),n=e.target.closest(".calendar-event");if(t instanceof HTMLElement){e.preventDefault();const i=t.getAttribute("data-calendar-id"),o=i?this.manager.getCalendar(i):null;o&&((a=this.contextMenu)==null||a.show(o,e.clientX,e.clientY))}else if(n instanceof HTMLElement){e.preventDefault();const i=n.getAttribute("data-event-id"),o=i?this.manager.getEvent(i):null;o&&((r=this.contextMenu)==null||r.show(o,e.clientX,e.clientY))}}handleContextMenuAction(e,t){var a,r,i,o,s,c,h,u,f,C,m;const n=()=>this.refreshCalendar();if("events"in t)switch(e){case"add-event":(a=this.menu)==null||a.showCreateEvent(t.id,n);break;case"edit-calendar":(r=this.menu)==null||r.showEditCalendarForm(t);break;case"copy-calendar":try{this.manager.copyCalendar(t.id),(o=this.editor)==null||o.showSuccessNotification(((i=this.editor)==null?void 0:i.t("Calendar copied successfully"))||"Calendar copied successfully"),this.refreshCalendar()}catch{(c=this.editor)==null||c.showErrorNotification(((s=this.editor)==null?void 0:s.t("Failed to copy calendar"))||"Failed to copy calendar")}break;case"export-calendar":this.showExportDialog(t);break;case"import-calendar":this.showImportDialog();break;case"delete-calendar":this.manager.deleteCalendar(t.id),this.refreshCalendar();break}else switch(e){case"edit-event":(h=this.menu)==null||h.showEditEvent(t,n);break;case"copy-event":try{this.manager.copyEvent(t.id),(f=this.editor)==null||f.showSuccessNotification(((u=this.editor)==null?void 0:u.t("Event copied successfully"))||"Event copied successfully"),this.refreshCalendar()}catch{(m=this.editor)==null||m.showErrorNotification(((C=this.editor)==null?void 0:C.t("Failed to copy event"))||"Failed to copy event")}break;case"delete-event":this.manager.deleteEvent(t.id),this.refreshCalendar();break}}showCalendarMenu(){var t;if(!this.editor)return;const e=this.editor.saveCursorPosition();(t=this.menu)==null||t.show(n=>{this.editor&&(e&&this.editor.restoreCursorPosition(e),this.insertCalendar(n))})}insertCalendar(e){if(!this.editor)return;const t=this.manager.generateCalendarHTML(e);this.editor.insertContent(t),this.editor.insertContent(k.createLineBreak())}refreshCalendar(){var t;const e=(t=this.editor)==null?void 0:t.getContainer().querySelectorAll(".calendar-widget");e==null||e.forEach(n=>{const a=n.getAttribute("data-calendar-id");if(!a)return;const r=this.manager.getCalendar(a);if(!r)return;const i=this.manager.generateCalendarHTML(r),o=document.createElement("div");o.innerHTML=i;const s=o.firstElementChild;s&&n.parentNode&&n.parentNode.replaceChild(s,n)})}showExportDialog(e){var t;try{const n=JSON.stringify(e,null,2),a=new Blob([n],{type:"application/json"}),r=URL.createObjectURL(a),i=document.createElement("a");i.href=r,i.download=`calendar-${e.id}.json`,i.click(),URL.revokeObjectURL(r)}catch{(t=this.editor)==null||t.showErrorNotification(this.editor.t("Export failed")||"Export failed")}}showImportDialog(){const e=document.createElement("input");e.type="file",e.accept=".json",e.onchange=t=>{var a;const n=(a=t.target.files)==null?void 0:a[0];if(n){const r=new FileReader;r.onload=i=>{var o,s,c,h,u;try{const f=JSON.parse((o=i.target)==null?void 0:o.result);this.manager.importCalendar(f),(c=this.editor)==null||c.showSuccessNotification(((s=this.editor)==null?void 0:s.t("Calendar imported successfully"))||"Calendar imported successfully"),this.refreshCalendar()}catch{(u=this.editor)==null||u.showErrorNotification(((h=this.editor)==null?void 0:h.t("Import failed"))||"Import failed")}},r.readAsText(n)}},e.click()}destroy(){var e;this.toolbarButton&&this.toolbarButton.parentElement&&this.toolbarButton.parentElement.removeChild(this.toolbarButton),this.menu&&(this.menu.destroy(),this.menu=null),this.contextMenu&&(this.contextMenu.destroy(),this.contextMenu=null),(e=this.editor)==null||e.off("calendar"),this.editor=null,this.manager=null,this.toolbarButton=null}}exports.CalendarPlugin=x;