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
80 lines (79 loc) • 2.97 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 */
var k = Object.defineProperty;
var h = (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) => h(i, typeof t != "symbol" ? t + "" : t, e);
import { createContainer as s } from "../../../utils/helpers.mjs";
class B {
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 n;
const e = s("editor-block text-block");
e.setAttribute("contenteditable", "false"), e.setAttribute("data-block-type", "text");
const o = s("block-content");
o.contentEditable = "true";
const c = [];
for (const a of t) {
const r = a.querySelector(".block-content");
r && ((n = r.textContent) != null && n.trim()) && c.push(r.textContent.trim());
}
return o.textContent = c.join(`
`), e.appendChild(o), e;
}
focusBlock(t) {
const e = t.querySelector(".block-content");
e && e.contentEditable === "true" && (e.contentEditable = "true", t.classList.add("active"));
}
}
export {
B as MergeBlocksCommand
};