@opentiny/fluent-editor
Version:
A rich text editor based on Quill 2.0, which extends rich modules and formats on the basis of Quill. It's powerful and out-of-the-box.
111 lines (110 loc) • 4.16 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import "./awareness/index.es.js";
import { setupIndexedDB } from "./awareness/y-indexeddb.es.js";
import { createProvider } from "./provider/providerRegistry.es.js";
import { setupAwareness, bindAwarenessToCursors } from "./awareness/awareness.es.js";
class CollaborativeEditor {
constructor(quill, options) {
__publicField(this, "ydoc");
__publicField(this, "provider");
__publicField(this, "awareness");
__publicField(this, "cursors");
__publicField(this, "cleanupBindings", null);
__publicField(this, "clearIndexedDB", null);
__publicField(this, "deps");
this.quill = quill;
this.options = options;
this.deps = this.options.deps || window;
const { Y, Awareness, QuillBinding, QuillCursors } = this.deps;
if (!Y || !Awareness || !QuillBinding || !QuillCursors) {
throw new Error(
"Missing required dependencies for collaborative editing. Please provide Y, Awareness, QuillBinding, and QuillCursors in the deps option."
);
}
this.ydoc = this.options.ydoc || new Y.Doc();
if (this.options.cursors !== false) {
const cursorsOptions = typeof this.options.cursors === "object" ? this.options.cursors : {};
this.cursors = new QuillCursors(quill, cursorsOptions);
}
if (this.options.awareness) {
const awareness = setupAwareness(this.options.awareness, new Awareness(this.ydoc));
if (!awareness) {
throw new Error("Failed to initialize awareness");
}
this.awareness = awareness;
} else {
this.awareness = new Awareness(this.ydoc);
}
if (this.options.provider) {
const providerConfig = this.options.provider;
try {
const provider = createProvider({
doc: this.ydoc,
options: providerConfig.options,
type: providerConfig.type,
awareness: this.awareness,
onConnect: this.options.onConnect,
onDisconnect: this.options.onDisconnect,
onError: this.options.onError,
onSyncChange: this.options.onSyncChange,
deps: this.deps
});
this.provider = provider;
} catch (error) {
console.warn(
`[yjs] Error creating provider of type ${providerConfig.type}:`,
error
);
}
}
if (this.provider) {
const ytext = this.ydoc.getText("tiny-editor");
this.cleanupBindings = bindAwarenessToCursors(this.awareness, this.cursors, quill, ytext, Y) || null;
new QuillBinding(
ytext,
this.quill,
this.awareness
);
} else {
console.error("Failed to initialize collaborative editor: no valid provider configured");
}
if (this.options.offline !== false) {
this.clearIndexedDB = setupIndexedDB(this.ydoc, this.deps.IndexeddbPersistence);
}
}
getAwareness() {
return this.awareness;
}
getProvider() {
return this.provider;
}
getYDoc() {
return this.ydoc;
}
get isConnected() {
var _a;
return ((_a = this.provider) == null ? void 0 : _a.isConnected) ?? false;
}
get isSynced() {
var _a;
return ((_a = this.provider) == null ? void 0 : _a.isSynced) ?? false;
}
getCursors() {
return this.cursors;
}
async destroy() {
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
(_a = this.cleanupBindings) == null ? void 0 : _a.call(this);
(_c = (_b = this.provider) == null ? void 0 : _b.destroy) == null ? void 0 : _c.call(_b);
(_d = this.cursors) == null ? void 0 : _d.clearCursors();
(_f = (_e = this.awareness) == null ? void 0 : _e.destroy) == null ? void 0 : _f.call(_e);
(_g = this.clearIndexedDB) == null ? void 0 : _g.call(this);
(_i = (_h = this.ydoc) == null ? void 0 : _h.destroy) == null ? void 0 : _i.call(_h);
}
}
export {
CollaborativeEditor
};
//# sourceMappingURL=collaborative-editing.es.js.map