Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/collaboration-yjs/src/connection-handler/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,25 @@ export class ConnectionHandler {
* Connect some listeners for our hooks, if provided
*/
if (this.#hooks.beforeChange) {
// keeping transaction here for future reference
// eslint-disable-next-line no-unused-vars
sharedDoc.on('beforeTransaction', (transaction) => {
this.#hooks.beforeChange(userParams);
});
}

if (this.#hooks.change) {
// keeping origin here for future reference
// eslint-disable-next-line no-unused-vars
sharedDoc.on('update', (update, origin) => {
this.#hooks.change(userParams);
});
}

socket.on('close', (code, reason) => {
this.#log('🔌 Socket closed, cleaning up connection for', params.documentId);
this.documentManager.releaseConnection(params.documentId, socket);
// eslint-disable-next-line no-unused-vars
socket.on('close', (/** @type {number} */ code, /** @type {Buffer} */ reason) => {
this.#log('🔌 Socket closed, cleaning up connection for', /** @type {string} */ (params.documentId));
this.documentManager.releaseConnection(/** @type {string} */ (params.documentId), socket);
});

/** Initialieze the socket connection */
Expand Down
12 changes: 6 additions & 6 deletions packages/collaboration-yjs/src/shared-doc/shared-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ export class SharedSuperDoc extends YDoc {
debouncer(() => callbackHandler(/** @type {SharedSuperDoc} */ (doc)));
});
}
this.whenInitialized = contentInitializer(this);

this.whenInitialized = contentInitializer();
}
}

/**
* @type {(ydoc: YDoc) => Promise<void>}
* @type {() => Promise<void>}
*/
let contentInitializer = (_ydoc) => Promise.resolve();
let contentInitializer = () => Promise.resolve();

/**
* The main handler for updates to the Yjs document.
* This function encodes the update and sends it to all connected clients.
* @param {Uint8Array} update
* @param {any} _origin
* @param {SharedSuperDoc} doc
* @param {any} _tr
*/
const updateHandler = (update, _origin, doc, _tr) => {
const updateHandler = (update, _origin, doc) => {
const encoder = createEncoder();
writeVarUint(encoder, messageSync);
writeUpdate(encoder, update);
Expand Down Expand Up @@ -121,7 +121,7 @@ export const send = (doc, conn, message) => {
conn.send(message, {}, (err) => {
err != null && closeConn(doc, conn);
});
} catch (e) {
} catch {
closeConn(doc, conn);
}
};
Expand Down
16 changes: 8 additions & 8 deletions packages/super-editor/src/components/toolbar/ai-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function getJsonBetweenFencesFromResponse(buffer) {
}

return null;
} catch (e) {
} catch {
return null;
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ export async function rewriteStreaming(text, prompt = '', options = {}, onChunk,
}

const message = prompt
? `Rewrite the following text: "${text}". Instructions: ${prompt}`
? `Rewrite the following text: "${text}" using these instructions: ${prompt}`
Comment thread
harbournick marked this conversation as resolved.
: `Rewrite the following text: "${text}"`;

const payload = {
Expand All @@ -248,7 +248,7 @@ export async function rewriteStreaming(text, prompt = '', options = {}, onChunk,
{
type: 'custom_prompt',
name: 'text_rewrite',
message: `Rewrite the following text: "${text}" using these instructions: ${prompt}`,
message,
},
],
};
Expand Down Expand Up @@ -276,7 +276,7 @@ export async function rewrite(text, prompt = '', options = {}) {
}

const message = prompt
? `Rewrite the following text: "${text}". Instructions: ${prompt}`
? `Rewrite the following text: "${text}" using these instructions: ${prompt}`
: `Rewrite the following text: "${text}"`;
Comment thread
harbournick marked this conversation as resolved.

const payload = {
Expand All @@ -286,7 +286,7 @@ export async function rewrite(text, prompt = '', options = {}) {
{
type: 'custom_prompt',
name: 'text_rewrite',
message: `Rewrite the following text: "${text}" using these instructions: ${prompt}`,
message,
format: [{ value: '' }],
},
],
Expand All @@ -306,7 +306,7 @@ const formatRegistry = {
{
name: 'bold',
pattern: /\*\*(.*?)\*\*/g,
transform: (_match, content, _editor) => ({
transform: (_match, content) => ({
type: 'text',
marks: [{ type: 'bold' }],
text: content,
Expand All @@ -315,7 +315,7 @@ const formatRegistry = {
{
name: 'italic',
pattern: /\*(.*?)\*/g,
transform: (_match, content, _editor) => ({
transform: (_match, content) => ({
type: 'text',
marks: [{ type: 'italic' }],
text: content,
Expand All @@ -324,7 +324,7 @@ const formatRegistry = {
{
name: 'underline',
pattern: /<(?:u|ins)>(.*?)<\/(?:u|ins)>/g,
transform: (_match, content, _editor) => ({
transform: (_match, content) => ({
type: 'text',
marks: [{ type: 'underline' }],
text: content,
Expand Down
6 changes: 1 addition & 5 deletions packages/super-editor/src/components/toolbar/defaultItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,6 @@ export const makeDefaultItems = ({
},
});

const toolbarItemsMobile = [bold, italic, underline, indentRight, indentLeft, search, overflow].map(
(item) => item.name,
);

const copyFormat = useToolbarItem({
type: 'button',
name: 'copyFormat',
Expand Down Expand Up @@ -1191,7 +1187,7 @@ export const makeDefaultItems = ({

export const setHistoryButtonStateOnUpdate =
(toolbarItemsRef) =>
({ editor, transaction }) => {
({ editor }) => {
// console.debug('[SuperEditor dev] Document updated', editor);
// activeEditor = editor;

Expand Down
32 changes: 19 additions & 13 deletions packages/super-editor/src/components/toolbar/super-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,18 @@ export class SuperToolbar extends EventEmitter {

/**
* Toggles the ruler visibility
* @param {Object} [_params=null] - Command parameters (not used)
* @returns {void}
*/
toggleRuler: (_params = null) => {
toggleRuler: () => {
this.superdoc.toggleRuler();
},

/**
* Initiates the image upload process
* @async
* @param {Object} [_params=null - Command parameters (not used)
* @returns {Promise<void>}
*/
startImageUpload: async (_params = null) => {
startImageUpload: async () => {
let open = getFileOpener();
let result = await open();

Expand Down Expand Up @@ -521,12 +519,11 @@ export class SuperToolbar extends EventEmitter {
/**
* Executes a table-related command
* @param {Object} params - Command parameters
* @param {CommandItem} params.item - The command item
* @param {Object} params.argument - The table command and its parameters
* @param {string} params.argument.command - The specific table command to execute
* @returns {void}
*/
executeTableCommand: ({ item, argument }) => {
executeTableCommand: ({ argument }) => {
if (!argument) return;

let command = argument.command;
Expand Down Expand Up @@ -733,8 +730,13 @@ export class SuperToolbar extends EventEmitter {
const linkedStyles = this.activeEditor.converter?.linkedStyles.find(
(style) => style.id === styleIdMark.attrs.styleId,
);
if (linkedStyles && markToStyleMap[item.name.value] in linkedStyles?.definition.styles) {
const linkedStylesItem = linkedStyles?.definition.styles[markToStyleMap[item.name.value]];
if (
linkedStyles &&
linkedStyles.definition &&
linkedStyles.definition.styles &&
markToStyleMap[item.name.value] in linkedStyles.definition.styles
) {
const linkedStylesItem = linkedStyles.definition.styles[markToStyleMap[item.name.value]];
const value = {
[item.name.value]: linkedStylesItem,
};
Expand Down Expand Up @@ -815,11 +817,10 @@ export class SuperToolbar extends EventEmitter {
/**
* React to editor transactions. Might want to debounce this.
* @param {Object} params - Transaction parameters
* @param {Object} params.editor - The editor instance (not used)
* @param {Object} params.transaction - The transaction object
* @returns {void}
*/
onEditorTransaction({ editor, transaction }) {
onEditorTransaction({ transaction }) {
if (!transaction.docChanged && !transaction.selectionSet) return;
this.updateToolbarState();
}
Expand Down Expand Up @@ -849,7 +850,7 @@ export class SuperToolbar extends EventEmitter {
return this.#interceptedCommands[command]({ item, argument });
}

if (command in this.activeEditor?.commands) {
if (this.activeEditor && this.activeEditor.commands && command in this.activeEditor.commands) {
this.activeEditor.commands[command](argument);
}

Expand Down Expand Up @@ -882,14 +883,19 @@ export class SuperToolbar extends EventEmitter {
let command = item.command;
const noArgumentCommand = item.noArgumentCommand;

if (argument === 'none' && noArgumentCommand in this.activeEditor?.commands) {
if (
argument === 'none' &&
this.activeEditor &&
this.activeEditor.commands &&
noArgumentCommand in this.activeEditor.commands
) {
this.activeEditor.commands[noArgumentCommand]();
if (typeof callback === 'function' && noArgumentCallback) callback(argument);
this.updateToolbarState();
return;
}

if (command in this.activeEditor?.commands) {
if (this.activeEditor && this.activeEditor.commands && command in this.activeEditor.commands) {
this.activeEditor.commands[command](argument);
if (typeof callback === 'function') callback(argument);
this.updateToolbarState();
Expand Down
4 changes: 2 additions & 2 deletions packages/super-editor/src/core/DocxZipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DocxZipper {
/**
* Get all docx data from the zipped docx
*
* [Content_Types].xml
* [ContentTypes].xml
* _rels/.rels
* word/document.xml
* word/_rels/document.xml.rels
Expand All @@ -40,7 +40,7 @@ class DocxZipper {
const mediaObjects = {};
const validTypes = ['xml', 'rels'];
for (const file of files) {
const [_, zipEntry] = file;
const [, zipEntry] = file;

if (validTypes.some((validType) => zipEntry.name.endsWith(validType))) {
const content = await zipEntry.async('string');
Expand Down
27 changes: 10 additions & 17 deletions packages/super-editor/src/core/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,6 @@ export class Editor extends EventEmitter {
*/
isFocused = false;

/**
* CSS styles for the editor
* @private
*/
#css;

options = {
element: null,
selector: null,
Expand Down Expand Up @@ -268,9 +262,9 @@ export class Editor extends EventEmitter {
this.setOptions(options);

let modes = {
docx: () => this.#init(this.options),
text: () => this.#initRichText(this.options),
html: () => this.#initRichText(this.options),
docx: () => this.#init(),
text: () => this.#initRichText(),
html: () => this.#initRichText(),
default: () => {
console.log('Not implemented.');
},
Expand Down Expand Up @@ -321,10 +315,9 @@ export class Editor extends EventEmitter {
/**
* Initialize the editor with the given options
* @private
* @param {EditorOptions} options - Editor options
* @returns {void}
*/
#init(options) {
#init() {
this.#createExtensionService();
this.#createCommandService();
this.#createSchema();
Expand Down Expand Up @@ -386,8 +379,8 @@ export class Editor extends EventEmitter {
* @param {EditorOptions} options - Editor options
* @returns {void}
*/
#initRichText(options) {
if (!options.extensions || !options.extensions.length) {
#initRichText() {
if (!this.options.extensions || !this.options.extensions.length) {
this.options.extensions = getRichTextExtensions();
}

Expand Down Expand Up @@ -944,7 +937,7 @@ export class Editor extends EventEmitter {
let doc;

try {
const { mode, fragment, isHeadless, content, loadFromSchema } = this.options;
const { mode, fragment, content, loadFromSchema } = this.options;

if (mode === 'docx') {
if (loadFromSchema) {
Expand Down Expand Up @@ -1666,7 +1659,7 @@ export class Editor extends EventEmitter {
console.debug('🔗 [super-editor] Ending collaboration');
if (this.options.collaborationProvider) this.options.collaborationProvider.disconnect();
if (this.options.ydoc) this.options.ydoc.destroy();
} catch (error) {}
} catch {}
}

/**
Expand All @@ -1691,7 +1684,7 @@ export class Editor extends EventEmitter {
}
this.converter.headerEditors.length = 0;
this.converter.footerEditors.length = 0;
} catch (error) {}
} catch {}
}

/**
Expand All @@ -1700,7 +1693,7 @@ export class Editor extends EventEmitter {
* @param {Object} data - Document data
* @returns {boolean} Whether migrations are needed
*/
static checkIfMigrationsNeeded(data) {
static checkIfMigrationsNeeded() {
const dataVersion = version || 'initial';
const migrations = getNecessaryMigrations(dataVersion) || [];
console.debug('[checkVersionMigrations] Migrations needed:', dataVersion, migrations.length);
Expand Down
6 changes: 2 additions & 4 deletions packages/super-editor/src/core/InputRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,14 @@ export const inputRulesPlugin = ({ editor, rules }) => {
handlePaste(view, event, slice) {
const clipboard = event.clipboardData;
const html = clipboard.getData('text/html');
const text = clipboard.getData('text/plain');

// Allow specialised plugins (e.g., field-annotation) first shot.
const fieldAnnotationContent = slice.content.content.filter((item) => item.type.name === 'fieldAnnotation');
if (fieldAnnotationContent.length) {
return false;
}

return handleClipboardPaste({ editor, view }, html, text);
return handleClipboardPaste({ editor, view }, html);
},
},

Expand Down Expand Up @@ -371,10 +370,9 @@ export function sanitizeHtml(html, forbiddenTags = ['meta', 'svg', 'script', 'st
* @param {Editor} params.editor The SuperEditor instance.
* @param {View} params.view The ProseMirror view associated with the editor.
* @param {String} html HTML clipboard content (may be empty).
* @param {String} text Plain text clipboard content (may be empty).
* @returns {Boolean} Whether the paste was handled.
*/
export function handleClipboardPaste({ editor, view }, html, text) {
export function handleClipboardPaste({ editor, view }, html) {
let source;
if (!html) {
source = 'plain-text';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const increaseListIndent =
() =>
({ editor, tr }) => {
const { state } = editor;
const $from = state.selection.$from;
const currentNode = ListHelpers.getCurrentListItem(state);
if (!currentNode) return false;

Expand Down
Loading