json-joy
Version:
Collection of libraries for building collaborative editing apps.
70 lines (69 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewFormatting = exports.SavedFormatting = exports.EditableFormatting = void 0;
const json_crdt_patch_1 = require("../../../../json-crdt-patch");
const model_1 = require("../../../../json-crdt/model");
class EditableFormatting {
constructor(behavior, range, state) {
this.behavior = behavior;
this.range = range;
this.state = state;
}
conf() {
return;
}
validate() {
return this.behavior.data()?.validate?.(this) ?? 'fine';
}
}
exports.EditableFormatting = EditableFormatting;
/**
* Formatting is a specific application of known formatting option to a range of
* text. Formatting is composed of a specific {@link Slice} which stores the
* state (location, data) of the formatting and a {@link ToolbarSliceBehavior}
* which defines the formatting behavior.
*/
class SavedFormatting extends EditableFormatting {
/**
* @returns Unique key for this formatting. This is the hash of the slice.
* This is used to identify the formatting in the UI.
*/
key() {
return this.range.hash;
}
conf() {
const node = this.range.dataNode();
return node instanceof model_1.ObjApi ? node : undefined;
}
}
exports.SavedFormatting = SavedFormatting;
/**
* New formatting which is being created. Once created, it will be promoted to
* a {@link SavedFormatting} instance.
*/
class NewFormatting extends EditableFormatting {
constructor(behavior, range, state) {
super(behavior, range, state);
this.behavior = behavior;
this.range = range;
this.state = state;
this.save = () => {
const state = this.state;
state.newSlice.next(void 0);
const view = this.conf()?.view();
if (!view || typeof view !== 'object')
return;
if (!view.title)
delete view.title;
const et = state.surface.events.et;
et.format('tog', this.behavior.tag, 'many', view);
et.cursor({ move: [['focus', 'char', 0, true]] });
};
const schema = json_crdt_patch_1.s.obj({ conf: behavior.schema || json_crdt_patch_1.s.con(void 0) });
this.model = model_1.Model.create(schema);
}
conf() {
return this.model.api.obj(['conf']);
}
}
exports.NewFormatting = NewFormatting;