json-joy
Version:
Collection of libraries for building collaborative editing apps.
51 lines • 1.69 kB
JavaScript
import { Slice } from '../slice/Slice';
const forEachRange = (selection, callback) => {
const slices = [];
for (const cursor of selection) {
const slice = callback(cursor);
slices.push(slice);
}
return slices;
};
export class EditorSlices {
txt;
slices;
constructor(txt, slices) {
this.txt = txt;
this.slices = slices;
}
/**
* @todo Rename to `insMany`.
*/
insStack(type, data, selection) {
const { slices, txt } = this;
selection ||= txt.editor.cursors();
return forEachRange(selection, (range) => slices.insStack(range.range(), type, data));
}
insOne(type, data, selection) {
const { slices, txt } = this;
selection ||= txt.editor.cursors();
return forEachRange(selection, (range) => slices.insOne(range.range(), type, data));
}
insErase(type, data, selection) {
const { slices, txt } = this;
selection ||= txt.editor.cursors();
return forEachRange(selection, (range) => slices.insErase(range.range(), type, data));
}
insMarker(type, data, selection) {
const { slices, txt } = this;
const editor = txt.editor;
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);
return marker;
});
}
del(sliceOrId) {
this.slices.del(sliceOrId instanceof Slice ? sliceOrId.id : sliceOrId);
}
}
//# sourceMappingURL=EditorSlices.js.map