mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* fix: Viewers attributed as editors when their editor repairs remote changes * fix: Rescan after remote changes and assign block ids when parsing markdown * chore: Import uuid as generateUuid in ToggleBlock * refactor: Add RepairPlugin to repair only on local changes, checking remote changes for pending repairs * fix: Assign block ids on the server so patches keep existing ids, keep unchanged mention ids * fix: Store API edits in the editor's trailing node form
114 lines
3.5 KiB
TypeScript
114 lines
3.5 KiB
TypeScript
import type { EditorState, Transaction } from "prosemirror-state";
|
|
import { PluginKey } from "prosemirror-state";
|
|
import type { DecorationSet } from "prosemirror-view";
|
|
import { recreateTransform } from "./prosemirror-recreate-transform";
|
|
|
|
/**
|
|
* Collaboration operations exposed by the facade plugin mounted by the
|
|
* multiplayer extension. They allow shared code to use the collaboration
|
|
* libraries without a static dependency on them.
|
|
*/
|
|
export interface MultiplayerOperations {
|
|
/** Returns true if the transaction originated from a remote client. */
|
|
isRemoteTransaction: (tr: Transaction) => boolean;
|
|
/** Stops the collaborative undo manager from capturing further changes. */
|
|
stopCapturing: (state: EditorState) => void;
|
|
}
|
|
|
|
/**
|
|
* Spec of the facade plugin mounted by the multiplayer extension.
|
|
*/
|
|
interface MultiplayerPluginSpec {
|
|
multiplayer?: MultiplayerOperations;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
/**
|
|
* Key of the facade plugin mounted by the multiplayer extension.
|
|
*/
|
|
export const multiplayerPluginKey = new PluginKey("multiplayer-facade");
|
|
|
|
/**
|
|
* Get the collaboration operations, if the multiplayer extension is mounted in
|
|
* the editor.
|
|
*
|
|
* @param state The editor state
|
|
* @returns the operations, or undefined when not collaborating
|
|
*/
|
|
function getOperations(state: EditorState): MultiplayerOperations | undefined {
|
|
const spec: MultiplayerPluginSpec | undefined =
|
|
multiplayerPluginKey.get(state)?.spec;
|
|
return spec?.multiplayer;
|
|
}
|
|
|
|
/**
|
|
* Checks if a transaction is a remote transaction
|
|
*
|
|
* @param tr The Prosemirror transaction
|
|
* @param state The editor state
|
|
* @returns true if the transaction is a remote transaction
|
|
*/
|
|
export function isRemoteTransaction(
|
|
tr: Transaction,
|
|
state: EditorState
|
|
): boolean {
|
|
return getOperations(state)?.isRemoteTransaction(tr) ?? false;
|
|
}
|
|
|
|
/**
|
|
* Checks if a transaction changed the document locally. Plugins that repair
|
|
* the document must only respond to local changes, otherwise every connected
|
|
* client writes the repair and is attributed as an editor.
|
|
*
|
|
* @param tr The Prosemirror transaction
|
|
* @param state The editor state
|
|
* @returns true if the transaction changed the document and is not remote
|
|
*/
|
|
export function isLocalDocChange(tr: Transaction, state: EditorState): boolean {
|
|
return tr.docChanged && !isRemoteTransaction(tr, state);
|
|
}
|
|
|
|
/**
|
|
* Stops the collaborative undo manager from capturing further changes, if
|
|
* the multiplayer extension is mounted in the editor.
|
|
*
|
|
* @param state The editor state
|
|
*/
|
|
export function stopCapturingUndo(state: EditorState): void {
|
|
getOperations(state)?.stopCapturing(state);
|
|
}
|
|
|
|
/**
|
|
* Map the set of decorations in response to a change in the document.
|
|
*
|
|
* @param set The current set of decorations
|
|
* @param tr The Prosemirror transaction
|
|
* @param state The editor state
|
|
* @param force Whether to force recalculation for map even for local transactions
|
|
* @returns The mapped set of decorations
|
|
*/
|
|
export function mapDecorations(
|
|
set: DecorationSet,
|
|
tr: Transaction,
|
|
state: EditorState,
|
|
force: boolean = false
|
|
): DecorationSet {
|
|
let mapping = tr.mapping;
|
|
const hasDecorations = set.find().length;
|
|
|
|
if (hasDecorations && (isRemoteTransaction(tr, state) || force)) {
|
|
try {
|
|
mapping = recreateTransform(tr.before, tr.doc, {
|
|
complexSteps: true,
|
|
wordDiffs: false,
|
|
simplifyDiff: true,
|
|
}).mapping;
|
|
} catch (err) {
|
|
// oxlint-disable-next-line no-console
|
|
console.warn("Failed to recreate transform: ", err);
|
|
}
|
|
}
|
|
|
|
return set.map(mapping, tr.doc);
|
|
}
|