UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

37 lines 1.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.slice = exports.step = exports.type = void 0; const json_crdt_patch_1 = require("../../../json-crdt-patch"); const constants_1 = require("./constants"); const type = (sliceType) => Array.isArray(sliceType) ? json_crdt_patch_1.s.arr(sliceType.map(exports.step)) : json_crdt_patch_1.s.con(sliceType); exports.type = type; const step = (sliceStep) => { if (Array.isArray(sliceStep)) { const tuple = [json_crdt_patch_1.s.con(sliceStep[0])]; const length = sliceStep.length; if (length > 1) tuple.push(json_crdt_patch_1.s.con(+(sliceStep[1] ?? 0))); if (length > 2) tuple.push(json_crdt_patch_1.s.json(sliceStep[2] ?? {})); return json_crdt_patch_1.s.vec(...tuple); } return json_crdt_patch_1.s.con(sliceStep); }; exports.step = step; const slice = (range, stacking, sliceType, data) => { const { start, end } = range; const header = (stacking << constants_1.SliceHeaderShift.Stacking) + ((start.anchor & 0b1) << constants_1.SliceHeaderShift.X1Anchor) + ((end.anchor & 0b1) << constants_1.SliceHeaderShift.X2Anchor); const elements = [ json_crdt_patch_1.s.con(header), json_crdt_patch_1.s.con(start.id), json_crdt_patch_1.s.con(!(0, json_crdt_patch_1.compare)(start.id, end.id) ? 0 : end.id), (0, exports.type)(sliceType), ]; if (data !== void 0) elements.push(json_crdt_patch_1.s.json(data)); return json_crdt_patch_1.s.vec(...elements); }; exports.slice = slice; //# sourceMappingURL=schema.js.map