UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

53 lines (52 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EditorSlices = void 0; const PersistedSlice_1 = require("../slice/PersistedSlice"); const forEachRange = (selection, callback) => { const slices = []; for (const cursor of selection) { const slice = callback(cursor); slices.push(slice); } return slices; }; class EditorSlices { constructor(txt, slices) { this.txt = txt; this.slices = slices; } /** * @todo Rename to `insMany`. */ insStack(type, data, selection) { const { slices, txt } = this; selection || (selection = txt.editor.cursors()); return forEachRange(selection, (range) => slices.insStack(range.range(), type, data)); } insOne(type, data, selection) { const { slices, txt } = this; selection || (selection = txt.editor.cursors()); return forEachRange(selection, (range) => slices.insOne(range.range(), type, data)); } insErase(type, data, selection) { const { slices, txt } = this; selection || (selection = txt.editor.cursors()); return forEachRange(selection, (range) => slices.insErase(range.range(), type, data)); } insMarker(type, data, separator, selection) { const { slices, txt } = this; const editor = txt.editor; selection || (selection = txt.editor.cursors()); return forEachRange(selection, (range) => { editor.collapseCursor(range); const after = range.start.clone(); after.refAfter(); const marker = slices.insMarkerAfter(after.id, type, data, separator); return marker; }); } del(sliceOrId) { this.slices.del(sliceOrId instanceof PersistedSlice_1.PersistedSlice ? sliceOrId.id : sliceOrId); } } exports.EditorSlices = EditorSlices;