json-joy
Version:
Collection of libraries for building collaborative editing apps.
71 lines • 2.17 kB
JavaScript
import { s } from '../../../../json-crdt-patch';
import { Model, ObjApi } from '../../../../json-crdt/model';
export class EditableFormatting {
behavior;
range;
state;
constructor(behavior, range, state) {
this.behavior = behavior;
this.range = range;
this.state = state;
}
conf() {
return;
}
validate() {
return this.behavior.data()?.validate?.(this) ?? 'fine';
}
}
/**
* 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.
*/
export 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 ObjApi ? node : undefined;
}
}
/**
* New formatting which is being created. Once created, it will be promoted to
* a {@link SavedFormatting} instance.
*/
export class NewFormatting extends EditableFormatting {
behavior;
range;
state;
model;
constructor(behavior, range, state) {
super(behavior, range, state);
this.behavior = behavior;
this.range = range;
this.state = state;
const schema = s.obj({ conf: behavior.schema || s.con(void 0) });
this.model = Model.create(schema);
}
conf() {
return this.model.api.obj(['conf']);
}
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]] });
};
}
//# sourceMappingURL=formattings.js.map