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

4 lines (3 loc) 2.47 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 k=Object.defineProperty;var d=(i,t,e)=>t in i?k(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var l=(i,t,e)=>d(i,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("../../../utils/helpers.cjs");class h{constructor(t){l(this,"name","mergeBlocks");l(this,"blocks",[]);l(this,"mergedBlock",null);l(this,"originalBlocks",[])}setData(t){this.blocks=t.blocks}execute(){var e;if(this.blocks.length<2||!this.areValidAdjacentBlocks(this.blocks))return;this.originalBlocks=[...this.blocks],this.mergedBlock=this.createMergedBlock(this.blocks);const t=this.blocks[0];(e=t.parentNode)==null||e.replaceChild(this.mergedBlock,t);for(let o=1;o<this.blocks.length;o++)this.blocks[o].remove();this.focusBlock(this.mergedBlock)}undo(){var t,e;if(this.mergedBlock&&this.originalBlocks.length>0){(t=this.mergedBlock.parentNode)==null||t.replaceChild(this.originalBlocks[0],this.mergedBlock);for(let o=1;o<this.originalBlocks.length;o++)(e=this.mergedBlock.parentNode)==null||e.insertBefore(this.originalBlocks[o],this.originalBlocks[o-1].nextSibling)}}redo(){var t;if(this.mergedBlock&&this.originalBlocks.length>0){const e=this.originalBlocks[0];(t=e.parentNode)==null||t.replaceChild(this.mergedBlock,e);for(let o=1;o<this.originalBlocks.length;o++)this.originalBlocks[o].remove()}}areValidAdjacentBlocks(t){if(t.length<2)return!1;for(const e of t)if(!this.isValidBlock(e))return!1;for(let e=0;e<t.length-1;e++)if(t[e].nextElementSibling!==t[e+1])return!1;return!0}isValidBlock(t){return t.classList.contains("editor-block")&&t.getAttribute("data-block-type")==="text"}createMergedBlock(t){var c;const e=s.createContainer("editor-block text-block");e.setAttribute("contenteditable","false"),e.setAttribute("data-block-type","text");const o=s.createContainer("block-content");o.contentEditable="true";const n=[];for(const a of t){const r=a.querySelector(".block-content");r&&((c=r.textContent)!=null&&c.trim())&&n.push(r.textContent.trim())}return o.textContent=n.join(` `),e.appendChild(o),e}focusBlock(t){const e=t.querySelector(".block-content");e&&e.contentEditable==="true"&&(e.contentEditable="true",t.classList.add("active"))}}exports.MergeBlocksCommand=h;