UNPKG

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) 3.79 kB
/*! 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 m=Object.defineProperty;var p=(a,t,e)=>t in a?m(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var s=(a,t,e)=>p(a,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});;/* empty css */;/* empty css */const F=require("./services/FootnoteManager.cjs"),b=require("./components/FootnoteMenu.cjs"),C=require("../ToolbarPlugin/utils.cjs"),B=require("../../icons/footnote.svg.cjs"),r=require("../../utils/helpers.cjs");class E{constructor(){s(this,"name","footnotes");s(this,"hotkeys",[{keys:"Ctrl+Alt+X",description:"Insert footnote",command:"footnotes",icon:"👣"}]);s(this,"editor",null);s(this,"manager");s(this,"menu",null);s(this,"toolbarButton",null);this.manager=new F.FootnoteManager}initialize(t){this.menu=new b.FootnoteMenu(t),this.editor=t,this.addToolbarButton(),this.setupEventListeners(),this.editor.on("footnotes",()=>{this.insertFootnote()})}addToolbarButton(){var e,o;const t=(e=this.editor)==null?void 0:e.getToolbar();t&&(this.toolbarButton=C.createToolbarButton({icon:B.default,title:((o=this.editor)==null?void 0:o.t("Insert Footnote"))??"",onClick:()=>this.insertFootnote()}),t.appendChild(this.toolbarButton))}setupEventListeners(){if(!this.editor)return;this.editor.getContainer().addEventListener("click",e=>{const o=e.target.closest(".footnote-ref");if(o instanceof HTMLElement){const n=o.getAttribute("data-footnote-id");n&&this.editFootnote(n)}})}insertFootnote(){var e,o;!this.editor||!((e=this.editor.getTextFormatter())!=null&&e.getSelection())||(o=this.menu)==null||o.show(n=>{const i=this.manager.createFootnote(n);this.insertFootnoteReference(i.id),this.updateFootnotesList()})}editFootnote(t){var o;const e=this.manager.getFootnote(t);e&&((o=this.menu)==null||o.show(n=>{this.manager.updateFootnote(t,n),this.updateFootnotesList()},e.content))}insertFootnoteReference(t){var n;if(!this.editor)return;const e=r.createSup("footnote-ref");e.className="footnote-ref",e.setAttribute("data-footnote-id",t),e.textContent=`[${this.manager.getFootnoteNumber(t)}]`;const o=(n=this.editor.getTextFormatter())==null?void 0:n.getSelection();if(o&&o.rangeCount>0){const i=o.getRangeAt(0);i.deleteContents(),i.insertNode(e),i.collapse(!1)}}updateFootnotesList(){var h;if(!this.editor)return;const t=this.editor.getContainer(),e=t.querySelector(".footnotes-section");e&&e.remove();const o=this.manager.getAllFootnotes();if(o.length===0)return;const n=r.createContainer();n.className="footnotes-section";const i=r.createHr();i.className="my-8",n.appendChild(i);const u=r.createH("h2","text-xl font-semibold mb-4",((h=this.editor)==null?void 0:h.t("Footnotes"))??"");n.appendChild(u);const d=r.createOl("footnotes-list");o.forEach(f=>{const c=r.createLi();c.id=`footnote-${f.id}`,f.content.split(/\r?\n/).filter(l=>l.trim()).forEach(l=>{const g=r.createP("footnote-line",l);c.appendChild(g)}),d.appendChild(c)}),n.appendChild(d),t.appendChild(n)}destroy(){var t;if(this.editor&&this.editor.getContainer().removeEventListener("click",o=>{const n=o.target.closest(".footnote-ref");if(n instanceof HTMLElement){const i=n.getAttribute("data-footnote-id");i&&this.editFootnote(i)}}),this.toolbarButton&&this.toolbarButton.parentElement&&this.toolbarButton.parentElement.removeChild(this.toolbarButton),this.editor){const e=this.editor.getContainer().querySelector(".footnotes-section");e&&e.remove()}(t=this.editor)==null||t.off("footnotes"),this.editor=null,this.menu=null,this.toolbarButton=null,this.manager.destroy()}}exports.FootnotesPlugin=E;