json-joy
Version:
Collection of libraries for building collaborative editing apps.
90 lines (89 loc) • 3.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SavedShadowFormatting = exports.FormattingManageState = void 0;
const rxjs_1 = require("rxjs");
const formattings_1 = require("../../state/formattings");
const PersistedSlice_1 = require("../../../../../json-crdt-extensions/peritext/slice/PersistedSlice");
const rx_1 = require("../../../../util/rx");
const toSchema_1 = require("../../../../../json-crdt/schema/toSchema");
const JsonCrdtDiff_1 = require("../../../../../json-crdt-diff/JsonCrdtDiff");
const model_1 = require("../../../../../json-crdt/model");
class FormattingManageState {
constructor(state, inline) {
this.state = state;
this.inline = inline;
this.selected$ = new rxjs_1.BehaviorSubject(null);
this.view$ = new rxjs_1.BehaviorSubject('view');
this.editing$ = new rxjs_1.BehaviorSubject(undefined);
this.select = (formatting) => {
this.selected$.next(formatting);
};
this.switchToViewPanel = () => {
this.view$.next('view');
this.editing$.next(void 0);
};
this.switchToEditPanel = () => {
const selected = this.selected$.getValue();
if (!selected)
return;
this.view$.next('edit');
const formatting = new SavedShadowFormatting(selected);
this.editing$.next(formatting);
};
this.returnFromEditPanelAndSave = () => {
const shadowFormatting = this.editing$.getValue();
if (!shadowFormatting)
return;
const view = shadowFormatting.conf().view();
const formatting = shadowFormatting.saved;
const data = formatting.conf();
if (!data)
return;
const model = data.api.model;
const patch = new JsonCrdtDiff_1.JsonCrdtDiff(model).diff(data.node, view);
if (patch.ops.length)
model.applyPatch(patch);
this.switchToViewPanel();
this.state.surface.rerender();
};
}
getFormattings$(inline = this.inline) {
const state = this.state;
return (0, rx_1.subject)(state.surface.render$, () => {
const slices = inline?.p1.layers;
const res = [];
if (!slices)
return res;
const registry = state.txt.editor.getRegistry();
for (const slice of slices) {
const tag = slice.type;
if (typeof tag !== 'number' && typeof tag !== 'string')
continue;
const behavior = registry.get(tag);
if (!behavior)
continue;
const isConfigurable = !!behavior.schema;
if (!isConfigurable)
continue;
if (!(slice instanceof PersistedSlice_1.PersistedSlice))
continue;
res.push(new formattings_1.SavedFormatting(behavior, slice, state));
}
return res;
});
}
}
exports.FormattingManageState = FormattingManageState;
class SavedShadowFormatting extends formattings_1.SavedFormatting {
constructor(saved) {
super(saved.behavior, saved.range, saved.state);
this.saved = saved;
const nodeApi = saved.conf();
const schema = nodeApi ? (0, toSchema_1.toSchema)(nodeApi.node) : void 0;
this._model = model_1.Model.create(schema);
}
conf() {
return this._model.api.obj([]);
}
}
exports.SavedShadowFormatting = SavedShadowFormatting;