@tiptap/extension-unique-id 3.23.5 → 3.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +16 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +16 -5
- package/dist/index.js.map +1 -1
- package/package.json +20 -21
- package/src/generate-unique-ids.ts +8 -3
- package/src/unique-id.ts +26 -9
package/dist/index.cjs
CHANGED
|
@@ -107,8 +107,12 @@ var UniqueID = import_core.Extension.create({
|
|
|
107
107
|
if (!this.options.updateDocument) {
|
|
108
108
|
return;
|
|
109
109
|
}
|
|
110
|
-
const collaboration = this.editor.extensionManager.extensions.find(
|
|
111
|
-
|
|
110
|
+
const collaboration = this.editor.extensionManager.extensions.find(
|
|
111
|
+
(ext) => ext.name === "collaboration"
|
|
112
|
+
);
|
|
113
|
+
const collaborationCaret = this.editor.extensionManager.extensions.find(
|
|
114
|
+
(ext) => ext.name === "collaborationCaret"
|
|
115
|
+
);
|
|
112
116
|
const collabExtensions = [collaboration, collaborationCaret].filter(Boolean);
|
|
113
117
|
const collab = collabExtensions.find((ext) => {
|
|
114
118
|
var _a2;
|
|
@@ -148,8 +152,12 @@ var UniqueID = import_core.Extension.create({
|
|
|
148
152
|
return [];
|
|
149
153
|
}
|
|
150
154
|
const extensionStorage = this.storage;
|
|
151
|
-
const collaboration = this.editor.extensionManager.extensions.find(
|
|
152
|
-
|
|
155
|
+
const collaboration = this.editor.extensionManager.extensions.find(
|
|
156
|
+
(ext) => ext.name === "collaboration"
|
|
157
|
+
);
|
|
158
|
+
const collaborationCaret = this.editor.extensionManager.extensions.find(
|
|
159
|
+
(ext) => ext.name === "collaborationCaret"
|
|
160
|
+
);
|
|
153
161
|
const collabExtensions = [collaboration, collaborationCaret].filter(Boolean);
|
|
154
162
|
const collabWithProvider = collabExtensions.find((ext) => {
|
|
155
163
|
var _a;
|
|
@@ -341,7 +349,10 @@ function generateUniqueIds(doc, extensions) {
|
|
|
341
349
|
if (!uniqueIDExtension) {
|
|
342
350
|
throw new Error("UniqueID extension not found in the extensions array");
|
|
343
351
|
}
|
|
344
|
-
const schema = (0, import_core2.getSchema)([
|
|
352
|
+
const schema = (0, import_core2.getSchema)([
|
|
353
|
+
...extensions.filter((ext) => ext.name !== "uniqueID"),
|
|
354
|
+
uniqueIDExtension
|
|
355
|
+
]);
|
|
345
356
|
const { types: configuredTypes, attributeName, generateID } = uniqueIDExtension.options;
|
|
346
357
|
const types = configuredTypes === "all" ? Object.keys(schema.nodes).filter((type) => type !== "doc" && type !== "text") : configuredTypes;
|
|
347
358
|
const contentNode = import_model2.Node.fromJSON(schema, doc);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/unique-id.ts","../src/helpers/findDuplicates.ts","../src/generate-unique-ids.ts"],"sourcesContent":["import { UniqueID } from './unique-id.js'\n\nexport * from './generate-unique-ids.js'\nexport * from './unique-id.js'\n\nexport default UniqueID\n","import {\n type Extensions,\n combineTransactionSteps,\n Extension,\n findChildren,\n findChildrenInRange,\n getChangedRanges,\n splitExtensions,\n} from '@tiptap/core'\nimport type { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Fragment, Slice } from '@tiptap/pm/model'\nimport type { Transaction } from '@tiptap/pm/state'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { v4 as uuidv4 } from 'uuid'\n\nimport { findDuplicates } from './helpers/findDuplicates.js'\n\nexport type UniqueIDGenerationContext = {\n node: ProseMirrorNode\n pos: number\n}\n\nexport interface UniqueIDOptions {\n /**\n * The name of the attribute to add the unique ID to.\n * @default \"id\"\n */\n attributeName: string\n /**\n * The types of nodes to add unique IDs to.\n * Use `\"all\"` to add IDs to every node type except `doc` and `text`.\n * @default []\n */\n types: string[] | 'all'\n /**\n * The function that generates the unique ID. By default, a UUID v4 is\n * generated. However, you can provide your own function to generate the\n * unique ID based on the node type and the position.\n */\n generateID: (ctx: UniqueIDGenerationContext) => any\n /**\n * Ignore some mutations, for example applied from other users through the collaboration plugin.\n *\n * @default null\n */\n filterTransaction: ((transaction: Transaction) => boolean) | null\n /**\n * Whether to update the document by adding unique IDs to the nodes. Set this\n * property to `false` if the document is in `readonly` mode, is immutable, or\n * you don't want it to be modified.\n *\n * @default true\n */\n updateDocument: boolean\n}\n\nconst resolveTypes = (types: UniqueIDOptions['types'], extensions: Extensions): string[] => {\n if (types !== 'all') {\n return types\n }\n\n const { nodeExtensions } = splitExtensions(extensions)\n\n return nodeExtensions.map(extension => extension.name).filter(type => type !== 'doc' && type !== 'text')\n}\n\nexport const UniqueID = Extension.create<UniqueIDOptions>({\n name: 'uniqueID',\n\n // we’ll set a very high priority to make sure this runs first\n // and is compatible with `appendTransaction` hooks of other extensions\n priority: 10000,\n\n addOptions() {\n return {\n attributeName: 'id',\n types: [],\n generateID: () => uuidv4(),\n filterTransaction: null,\n updateDocument: true,\n }\n },\n\n /**\n * Extension storage for coordination between `addProseMirrorPlugins` and `appendTransaction`.\n * `needsInitialIdGeneration` is set to `true` when the Collaboration extension is\n * detected but no provider is available in its options, deferring ID creation\n * to the first `y-sync$` transaction.\n */\n addStorage() {\n return {\n needsInitialIdGeneration: false,\n }\n },\n\n addGlobalAttributes() {\n const types = resolveTypes(this.options.types, this.extensions)\n\n return [\n {\n types,\n attributes: {\n [this.options.attributeName]: {\n default: null,\n parseHTML: element => element.getAttribute(`data-${this.options.attributeName}`),\n renderHTML: attributes => {\n if (!attributes[this.options.attributeName]) {\n return {}\n }\n\n return {\n [`data-${this.options.attributeName}`]: attributes[this.options.attributeName],\n }\n },\n },\n },\n },\n ]\n },\n\n // check initial content for missing ids\n onCreate() {\n if (!this.options.updateDocument) {\n return\n }\n\n const collaboration = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaboration')\n const collaborationCaret = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaborationCaret')\n\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collab = collabExtensions.find(ext => ext?.options?.provider)\n const provider = collab?.options?.provider\n\n const createIds = () => {\n const { view, state } = this.editor\n const { tr, doc } = state\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n const { attributeName, generateID } = this.options\n const nodesWithoutId = findChildren(doc, node => {\n return types.includes(node.type.name) && node.attrs[attributeName] === null\n })\n\n nodesWithoutId.forEach(({ node, pos }) => {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n })\n\n tr.setMeta('addToHistory', false)\n\n view.dispatch(tr)\n\n if (provider) {\n provider.off('synced', createIds)\n }\n }\n\n /**\n * We need to handle collaboration a bit different here\n * because we can't automatically add IDs when the provider is not yet synced\n * otherwise we end up with empty paragraphs\n */\n if (collaboration) {\n if (provider) {\n provider.on('synced', createIds)\n }\n // When collaboration is present but no provider is in extension options,\n // needsInitialIdGeneration was already set in addProseMirrorPlugins\n // (which runs synchronously during editor construction, before any\n // y-sync$ transaction can arrive).\n } else {\n return createIds()\n }\n },\n\n addProseMirrorPlugins() {\n if (!this.options.updateDocument) {\n return []\n }\n\n // Capture storage via closure so appendTransaction can access `needsInitialIdGeneration`.\n // `extensionManager.extensions` returns different objects than `this` due to\n // Tiptap's child-extension flattening, so we capture the reference directly.\n const extensionStorage = this.storage\n\n // Detect collaboration early — before any y-sync$ transaction can arrive.\n // onCreate runs on a deferred setTimeout(0), so a y-sync$ transaction could\n // be applied before it. Setting the flag here (synchronous during editor\n // construction) ensures the first y-sync$ is always handled.\n const collaboration = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaboration')\n const collaborationCaret = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaborationCaret')\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collabWithProvider = collabExtensions.find(ext => ext?.options?.provider)\n\n if (collaboration && !collabWithProvider) {\n extensionStorage.needsInitialIdGeneration = true\n }\n\n let dragSourceElement: Element | null = null\n let transformPasted = false\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n\n return [\n new Plugin({\n key: new PluginKey('uniqueID'),\n\n appendTransaction: (transactions, oldState, newState) => {\n const hasDocChanges =\n transactions.some(transaction => transaction.docChanged) && !oldState.doc.eq(newState.doc)\n const filterTransactions =\n this.options.filterTransaction && transactions.some(tr => !this.options.filterTransaction?.(tr))\n\n const isCollabTransaction = transactions.find(tr => tr.getMeta('y-sync$'))\n\n if (isCollabTransaction) {\n if (extensionStorage.needsInitialIdGeneration) {\n extensionStorage.needsInitialIdGeneration = false\n\n // Run full-document ID creation after the first Yjs sync.\n // Use a seen-set so only the second+ occurrence of a duplicated\n // ID is regenerated, preserving one existing ID per value.\n const { tr } = newState\n const { attributeName, generateID } = this.options\n const allNodes = findChildren(newState.doc, node => types.includes(node.type.name))\n const seen = new Set<string>()\n\n allNodes.forEach(({ node, pos }) => {\n const currentId = node.attrs[attributeName]\n\n if (currentId === null || seen.has(currentId)) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n } else {\n seen.add(currentId)\n }\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // Restore stored marks since setNodeMarkup resets them\n tr.setStoredMarks(newState.tr.storedMarks)\n // Mark this transaction as coming from UniqueID to prevent\n // infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n tr.setMeta('addToHistory', false)\n return tr\n }\n\n return\n }\n\n if (!hasDocChanges || filterTransactions) {\n return\n }\n\n const { tr } = newState\n\n const { attributeName, generateID } = this.options\n const transform = combineTransactionSteps(oldState.doc, transactions as Transaction[])\n const { mapping } = transform\n\n // get changed ranges based on the old state\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ newRange }) => {\n const newNodes = findChildrenInRange(newState.doc, newRange, node => {\n return types.includes(node.type.name)\n })\n\n const newIds = newNodes.map(({ node }) => node.attrs[attributeName]).filter(id => id !== null)\n\n newNodes.forEach(({ node, pos }, i) => {\n // instead of checking `node.attrs[attributeName]` directly\n // we look at the current state of the node within `tr.doc`.\n // this helps to prevent adding new ids to the same node\n // if the node changed multiple times within one transaction\n const id = tr.doc.nodeAt(pos)?.attrs[attributeName]\n\n if (id === null) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n\n return\n }\n\n const nextNode = newNodes[i + 1]\n\n if (nextNode && node.content.size === 0) {\n const nextNodeInTr = tr.doc.nodeAt(nextNode.pos)\n if (nextNodeInTr?.attrs[attributeName] && nextNodeInTr.attrs[attributeName] !== id) {\n return\n }\n\n tr.setNodeMarkup(nextNode.pos, undefined, {\n ...nextNode.node.attrs,\n [attributeName]: id,\n })\n newIds[i + 1] = id\n\n const generatedId = generateID({ node, pos })\n\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generatedId,\n })\n newIds[i] = generatedId\n\n return tr\n }\n\n const duplicatedNewIds = findDuplicates(newIds)\n\n // check if the node doesn’t exist in the old state\n const { deleted } = mapping.invert().mapResult(pos)\n\n const newNode = deleted && duplicatedNewIds.includes(id)\n\n if (newNode) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n }\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // `tr.setNodeMarkup` resets the stored marks\n // so we'll restore them if they exist\n tr.setStoredMarks(newState.tr.storedMarks)\n\n // Mark this transaction as coming from UniqueID\n // to prevent infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n\n return tr\n },\n\n // we register a global drag handler to track the current drag source element\n view(view) {\n const handleDragstart = (event: DragEvent) => {\n dragSourceElement = view.dom.parentElement?.contains(event.target as Element)\n ? view.dom.parentElement\n : null\n }\n\n window.addEventListener('dragstart', handleDragstart)\n\n return {\n destroy() {\n window.removeEventListener('dragstart', handleDragstart)\n },\n }\n },\n\n props: {\n // `handleDOMEvents` is called before `transformPasted`\n // so we can do some checks before\n handleDOMEvents: {\n // only create new ids for dropped content\n // or dropped content while holding `alt`\n // or content is dragged from another editor\n drop: (view, event) => {\n if (\n dragSourceElement !== view.dom.parentElement ||\n event.dataTransfer?.effectAllowed === 'copyMove' ||\n event.dataTransfer?.effectAllowed === 'copy'\n ) {\n dragSourceElement = null\n transformPasted = true\n }\n\n return false\n },\n // always create new ids on pasted content\n paste: () => {\n transformPasted = true\n\n return false\n },\n },\n\n // we’ll remove ids for every pasted node\n // so we can create a new one within `appendTransaction`\n transformPasted: slice => {\n if (!transformPasted) {\n return slice\n }\n\n const { attributeName } = this.options\n const removeId = (fragment: Fragment): Fragment => {\n const list: ProseMirrorNode[] = []\n\n fragment.forEach(node => {\n // don’t touch text nodes\n if (node.isText) {\n list.push(node)\n\n return\n }\n\n // check for any other child nodes\n if (!types.includes(node.type.name)) {\n list.push(node.copy(removeId(node.content)))\n\n return\n }\n\n // remove id\n const nodeWithoutId = node.type.create(\n {\n ...node.attrs,\n [attributeName]: null,\n },\n removeId(node.content),\n node.marks,\n )\n\n list.push(nodeWithoutId)\n })\n\n return Fragment.from(list)\n }\n\n // reset check\n transformPasted = false\n\n return new Slice(removeId(slice.content), slice.openStart, slice.openEnd)\n },\n },\n }),\n ]\n },\n})\n","/**\n * Returns a list of duplicated items within an array.\n */\nexport function findDuplicates(items: any[]): any[] {\n const seen = new Set()\n const duplicates = new Set<any>()\n\n items.forEach(item => {\n if (seen.has(item)) {\n duplicates.add(item)\n } else {\n seen.add(item)\n }\n })\n\n return Array.from(duplicates)\n}\n","import type { Extensions, JSONContent } from '@tiptap/core'\nimport { findChildren, getSchema } from '@tiptap/core'\nimport { Node } from '@tiptap/pm/model'\nimport { EditorState } from '@tiptap/pm/state'\n\nimport type { UniqueID } from './unique-id.js'\n\n/**\n * Creates a new document with unique IDs added to the nodes. Does the same\n * thing as the UniqueID extension, but without the need to create an `Editor`\n * instance. This lets you add unique IDs to the document in the server.\n *\n * When you call it, include the `UniqueID` extension in the `extensions` array.\n * The configuration from the `UniqueID` extension will be picked up\n * automatically, including its configuration options like `types` and\n * `attributeName`.\n *\n * @see `UniqueID` extension for more information.\n *\n * @throws {Error} If the `UniqueID` extension is not found in the extensions array.\n *\n * @example\n * const doc = {\n * type: 'doc',\n * content: [\n * { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }] }\n * ]\n * }\n * const newDoc = addUniqueIds(doc, [StarterKit, UniqueID.configure({ types: ['paragraph', 'heading'] })])\n * console.log(newDoc)\n * // Result:\n * // {\n * // type: 'doc',\n * // content: [\n * // { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }], id: '123' }\n * // ]\n * // }\n *\n * @param doc - A Tiptap JSON document to add unique IDs to.\n * @param extensions - The extensions to use. Must include the `UniqueID` extension.\n * @returns The updated Tiptap JSON document, with the unique IDs added to the nodes.\n */\nexport function generateUniqueIds(doc: JSONContent, extensions: Extensions): JSONContent {\n // Find the UniqueID extension in the extensions array. If it's not found, throw an error.\n const uniqueIDExtension = extensions.find(ext => ext.name === 'uniqueID') as typeof UniqueID | undefined\n if (!uniqueIDExtension) {\n throw new Error('UniqueID extension not found in the extensions array')\n }\n // Convert the JSON content to a ProseMirror node\n const schema = getSchema([...extensions.filter(ext => ext.name !== 'uniqueID'), uniqueIDExtension])\n const { types: configuredTypes, attributeName, generateID } = uniqueIDExtension.options\n const types =\n configuredTypes === 'all'\n ? Object.keys(schema.nodes).filter(type => type !== 'doc' && type !== 'text')\n : configuredTypes\n const contentNode = Node.fromJSON(schema, doc)\n\n // Find nodes that don't have a unique ID\n const nodesWithoutId = findChildren(contentNode, node => {\n return !node.attrs[attributeName] && types.includes(node.type.name)\n })\n\n // Edit the document to add unique IDs to the nodes that don't have a unique ID\n let tr = EditorState.create({\n doc: contentNode,\n }).tr\n // eslint-disable-next-line no-restricted-syntax\n for (const { node, pos } of nodesWithoutId) {\n tr = tr.setNodeAttribute(pos, attributeName, generateID({ node, pos }))\n }\n\n // Return the updated document\n return tr.doc.toJSON()\n}\n"],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;AACN,QAAM,cAAc,mBAAK,SAAS,QAAQ,GAAG;AAG7C,QAAM,qBAAiB,2BAAa,aAAa,UAAQ;AACvD,WAAO,CAAC,KAAK,MAAM,aAAa,KAAK,MAAM,SAAS,KAAK,KAAK,IAAI;AAAA,EACpE,CAAC;AAGD,MAAI,KAAK,0BAAY,OAAO;AAAA,IAC1B,KAAK;AAAA,EACP,CAAC,EAAE;AAEH,aAAW,EAAE,MAAM,IAAI,KAAK,gBAAgB;AAC1C,SAAK,GAAG,iBAAiB,KAAK,eAAe,WAAW,EAAE,MAAM,IAAI,CAAC,CAAC;AAAA,EACxE;AAGA,SAAO,GAAG,IAAI,OAAO;AACvB;;;AHpEA,IAAO,gBAAQ;","names":["uuidv4","_a","tr","attributeName","generateID","import_core","import_model","import_state"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/unique-id.ts","../src/helpers/findDuplicates.ts","../src/generate-unique-ids.ts"],"sourcesContent":["import { UniqueID } from './unique-id.js'\n\nexport * from './generate-unique-ids.js'\nexport * from './unique-id.js'\n\nexport default UniqueID\n","import {\n type Extensions,\n combineTransactionSteps,\n Extension,\n findChildren,\n findChildrenInRange,\n getChangedRanges,\n splitExtensions,\n} from '@tiptap/core'\nimport type { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Fragment, Slice } from '@tiptap/pm/model'\nimport type { Transaction } from '@tiptap/pm/state'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { v4 as uuidv4 } from 'uuid'\n\nimport { findDuplicates } from './helpers/findDuplicates.js'\n\nexport type UniqueIDGenerationContext = {\n node: ProseMirrorNode\n pos: number\n}\n\nexport interface UniqueIDOptions {\n /**\n * The name of the attribute to add the unique ID to.\n * @default \"id\"\n */\n attributeName: string\n /**\n * The types of nodes to add unique IDs to.\n * Use `\"all\"` to add IDs to every node type except `doc` and `text`.\n * @default []\n */\n types: string[] | 'all'\n /**\n * The function that generates the unique ID. By default, a UUID v4 is\n * generated. However, you can provide your own function to generate the\n * unique ID based on the node type and the position.\n */\n generateID: (ctx: UniqueIDGenerationContext) => any\n /**\n * Ignore some mutations, for example applied from other users through the collaboration plugin.\n *\n * @default null\n */\n filterTransaction: ((transaction: Transaction) => boolean) | null\n /**\n * Whether to update the document by adding unique IDs to the nodes. Set this\n * property to `false` if the document is in `readonly` mode, is immutable, or\n * you don't want it to be modified.\n *\n * @default true\n */\n updateDocument: boolean\n}\n\nconst resolveTypes = (types: UniqueIDOptions['types'], extensions: Extensions): string[] => {\n if (types !== 'all') {\n return types\n }\n\n const { nodeExtensions } = splitExtensions(extensions)\n\n return nodeExtensions\n .map(extension => extension.name)\n .filter(type => type !== 'doc' && type !== 'text')\n}\n\nexport const UniqueID = Extension.create<UniqueIDOptions>({\n name: 'uniqueID',\n\n // we’ll set a very high priority to make sure this runs first\n // and is compatible with `appendTransaction` hooks of other extensions\n priority: 10000,\n\n addOptions() {\n return {\n attributeName: 'id',\n types: [],\n generateID: () => uuidv4(),\n filterTransaction: null,\n updateDocument: true,\n }\n },\n\n /**\n * Extension storage for coordination between `addProseMirrorPlugins` and `appendTransaction`.\n * `needsInitialIdGeneration` is set to `true` when the Collaboration extension is\n * detected but no provider is available in its options, deferring ID creation\n * to the first `y-sync$` transaction.\n */\n addStorage() {\n return {\n needsInitialIdGeneration: false,\n }\n },\n\n addGlobalAttributes() {\n const types = resolveTypes(this.options.types, this.extensions)\n\n return [\n {\n types,\n attributes: {\n [this.options.attributeName]: {\n default: null,\n parseHTML: element => element.getAttribute(`data-${this.options.attributeName}`),\n renderHTML: attributes => {\n if (!attributes[this.options.attributeName]) {\n return {}\n }\n\n return {\n [`data-${this.options.attributeName}`]: attributes[this.options.attributeName],\n }\n },\n },\n },\n },\n ]\n },\n\n // check initial content for missing ids\n onCreate() {\n if (!this.options.updateDocument) {\n return\n }\n\n const collaboration = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaboration',\n )\n const collaborationCaret = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaborationCaret',\n )\n\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collab = collabExtensions.find(ext => ext?.options?.provider)\n const provider = collab?.options?.provider\n\n const createIds = () => {\n const { view, state } = this.editor\n const { tr, doc } = state\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n const { attributeName, generateID } = this.options\n const nodesWithoutId = findChildren(doc, node => {\n return types.includes(node.type.name) && node.attrs[attributeName] === null\n })\n\n nodesWithoutId.forEach(({ node, pos }) => {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n })\n\n tr.setMeta('addToHistory', false)\n\n view.dispatch(tr)\n\n if (provider) {\n provider.off('synced', createIds)\n }\n }\n\n /**\n * We need to handle collaboration a bit different here\n * because we can't automatically add IDs when the provider is not yet synced\n * otherwise we end up with empty paragraphs\n */\n if (collaboration) {\n if (provider) {\n provider.on('synced', createIds)\n }\n // When collaboration is present but no provider is in extension options,\n // needsInitialIdGeneration was already set in addProseMirrorPlugins\n // (which runs synchronously during editor construction, before any\n // y-sync$ transaction can arrive).\n } else {\n return createIds()\n }\n },\n\n addProseMirrorPlugins() {\n if (!this.options.updateDocument) {\n return []\n }\n\n // Capture storage via closure so appendTransaction can access `needsInitialIdGeneration`.\n // `extensionManager.extensions` returns different objects than `this` due to\n // Tiptap's child-extension flattening, so we capture the reference directly.\n const extensionStorage = this.storage\n\n // Detect collaboration early — before any y-sync$ transaction can arrive.\n // onCreate runs on a deferred setTimeout(0), so a y-sync$ transaction could\n // be applied before it. Setting the flag here (synchronous during editor\n // construction) ensures the first y-sync$ is always handled.\n const collaboration = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaboration',\n )\n const collaborationCaret = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaborationCaret',\n )\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collabWithProvider = collabExtensions.find(ext => ext?.options?.provider)\n\n if (collaboration && !collabWithProvider) {\n extensionStorage.needsInitialIdGeneration = true\n }\n\n let dragSourceElement: Element | null = null\n let transformPasted = false\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n\n return [\n new Plugin({\n key: new PluginKey('uniqueID'),\n\n appendTransaction: (transactions, oldState, newState) => {\n const hasDocChanges =\n transactions.some(transaction => transaction.docChanged) &&\n !oldState.doc.eq(newState.doc)\n const filterTransactions =\n this.options.filterTransaction &&\n transactions.some(tr => !this.options.filterTransaction?.(tr))\n\n const isCollabTransaction = transactions.find(tr => tr.getMeta('y-sync$'))\n\n if (isCollabTransaction) {\n if (extensionStorage.needsInitialIdGeneration) {\n extensionStorage.needsInitialIdGeneration = false\n\n // Run full-document ID creation after the first Yjs sync.\n // Use a seen-set so only the second+ occurrence of a duplicated\n // ID is regenerated, preserving one existing ID per value.\n const { tr } = newState\n const { attributeName, generateID } = this.options\n const allNodes = findChildren(newState.doc, node => types.includes(node.type.name))\n const seen = new Set<string>()\n\n allNodes.forEach(({ node, pos }) => {\n const currentId = node.attrs[attributeName]\n\n if (currentId === null || seen.has(currentId)) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n } else {\n seen.add(currentId)\n }\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // Restore stored marks since setNodeMarkup resets them\n tr.setStoredMarks(newState.tr.storedMarks)\n // Mark this transaction as coming from UniqueID to prevent\n // infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n tr.setMeta('addToHistory', false)\n return tr\n }\n\n return\n }\n\n if (!hasDocChanges || filterTransactions) {\n return\n }\n\n const { tr } = newState\n\n const { attributeName, generateID } = this.options\n const transform = combineTransactionSteps(oldState.doc, transactions as Transaction[])\n const { mapping } = transform\n\n // get changed ranges based on the old state\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ newRange }) => {\n const newNodes = findChildrenInRange(newState.doc, newRange, node => {\n return types.includes(node.type.name)\n })\n\n const newIds = newNodes\n .map(({ node }) => node.attrs[attributeName])\n .filter(id => id !== null)\n\n newNodes.forEach(({ node, pos }, i) => {\n // instead of checking `node.attrs[attributeName]` directly\n // we look at the current state of the node within `tr.doc`.\n // this helps to prevent adding new ids to the same node\n // if the node changed multiple times within one transaction\n const id = tr.doc.nodeAt(pos)?.attrs[attributeName]\n\n if (id === null) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n\n return\n }\n\n const nextNode = newNodes[i + 1]\n\n if (nextNode && node.content.size === 0) {\n const nextNodeInTr = tr.doc.nodeAt(nextNode.pos)\n if (\n nextNodeInTr?.attrs[attributeName] &&\n nextNodeInTr.attrs[attributeName] !== id\n ) {\n return\n }\n\n tr.setNodeMarkup(nextNode.pos, undefined, {\n ...nextNode.node.attrs,\n [attributeName]: id,\n })\n newIds[i + 1] = id\n\n const generatedId = generateID({ node, pos })\n\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generatedId,\n })\n newIds[i] = generatedId\n\n return tr\n }\n\n const duplicatedNewIds = findDuplicates(newIds)\n\n // check if the node doesn’t exist in the old state\n const { deleted } = mapping.invert().mapResult(pos)\n\n const newNode = deleted && duplicatedNewIds.includes(id)\n\n if (newNode) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n }\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // `tr.setNodeMarkup` resets the stored marks\n // so we'll restore them if they exist\n tr.setStoredMarks(newState.tr.storedMarks)\n\n // Mark this transaction as coming from UniqueID\n // to prevent infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n\n return tr\n },\n\n // we register a global drag handler to track the current drag source element\n view(view) {\n const handleDragstart = (event: DragEvent) => {\n dragSourceElement = view.dom.parentElement?.contains(event.target as Element)\n ? view.dom.parentElement\n : null\n }\n\n window.addEventListener('dragstart', handleDragstart)\n\n return {\n destroy() {\n window.removeEventListener('dragstart', handleDragstart)\n },\n }\n },\n\n props: {\n // `handleDOMEvents` is called before `transformPasted`\n // so we can do some checks before\n handleDOMEvents: {\n // only create new ids for dropped content\n // or dropped content while holding `alt`\n // or content is dragged from another editor\n drop: (view, event) => {\n if (\n dragSourceElement !== view.dom.parentElement ||\n event.dataTransfer?.effectAllowed === 'copyMove' ||\n event.dataTransfer?.effectAllowed === 'copy'\n ) {\n dragSourceElement = null\n transformPasted = true\n }\n\n return false\n },\n // always create new ids on pasted content\n paste: () => {\n transformPasted = true\n\n return false\n },\n },\n\n // we’ll remove ids for every pasted node\n // so we can create a new one within `appendTransaction`\n transformPasted: slice => {\n if (!transformPasted) {\n return slice\n }\n\n const { attributeName } = this.options\n const removeId = (fragment: Fragment): Fragment => {\n const list: ProseMirrorNode[] = []\n\n fragment.forEach(node => {\n // don’t touch text nodes\n if (node.isText) {\n list.push(node)\n\n return\n }\n\n // check for any other child nodes\n if (!types.includes(node.type.name)) {\n list.push(node.copy(removeId(node.content)))\n\n return\n }\n\n // remove id\n const nodeWithoutId = node.type.create(\n {\n ...node.attrs,\n [attributeName]: null,\n },\n removeId(node.content),\n node.marks,\n )\n\n list.push(nodeWithoutId)\n })\n\n return Fragment.from(list)\n }\n\n // reset check\n transformPasted = false\n\n return new Slice(removeId(slice.content), slice.openStart, slice.openEnd)\n },\n },\n }),\n ]\n },\n})\n","/**\n * Returns a list of duplicated items within an array.\n */\nexport function findDuplicates(items: any[]): any[] {\n const seen = new Set()\n const duplicates = new Set<any>()\n\n items.forEach(item => {\n if (seen.has(item)) {\n duplicates.add(item)\n } else {\n seen.add(item)\n }\n })\n\n return Array.from(duplicates)\n}\n","import type { Extensions, JSONContent } from '@tiptap/core'\nimport { findChildren, getSchema } from '@tiptap/core'\nimport { Node } from '@tiptap/pm/model'\nimport { EditorState } from '@tiptap/pm/state'\n\nimport type { UniqueID } from './unique-id.js'\n\n/**\n * Creates a new document with unique IDs added to the nodes. Does the same\n * thing as the UniqueID extension, but without the need to create an `Editor`\n * instance. This lets you add unique IDs to the document in the server.\n *\n * When you call it, include the `UniqueID` extension in the `extensions` array.\n * The configuration from the `UniqueID` extension will be picked up\n * automatically, including its configuration options like `types` and\n * `attributeName`.\n *\n * @see `UniqueID` extension for more information.\n *\n * @throws {Error} If the `UniqueID` extension is not found in the extensions array.\n *\n * @example\n * const doc = {\n * type: 'doc',\n * content: [\n * { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }] }\n * ]\n * }\n * const newDoc = addUniqueIds(doc, [StarterKit, UniqueID.configure({ types: ['paragraph', 'heading'] })])\n * console.log(newDoc)\n * // Result:\n * // {\n * // type: 'doc',\n * // content: [\n * // { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }], id: '123' }\n * // ]\n * // }\n *\n * @param doc - A Tiptap JSON document to add unique IDs to.\n * @param extensions - The extensions to use. Must include the `UniqueID` extension.\n * @returns The updated Tiptap JSON document, with the unique IDs added to the nodes.\n */\nexport function generateUniqueIds(doc: JSONContent, extensions: Extensions): JSONContent {\n // Find the UniqueID extension in the extensions array. If it's not found, throw an error.\n const uniqueIDExtension = extensions.find(ext => ext.name === 'uniqueID') as\n | typeof UniqueID\n | undefined\n if (!uniqueIDExtension) {\n throw new Error('UniqueID extension not found in the extensions array')\n }\n // Convert the JSON content to a ProseMirror node\n const schema = getSchema([\n ...extensions.filter(ext => ext.name !== 'uniqueID'),\n uniqueIDExtension,\n ])\n const { types: configuredTypes, attributeName, generateID } = uniqueIDExtension.options\n const types =\n configuredTypes === 'all'\n ? Object.keys(schema.nodes).filter(type => type !== 'doc' && type !== 'text')\n : configuredTypes\n const contentNode = Node.fromJSON(schema, doc)\n\n // Find nodes that don't have a unique ID\n const nodesWithoutId = findChildren(contentNode, node => {\n return !node.attrs[attributeName] && types.includes(node.type.name)\n })\n\n // Edit the document to add unique IDs to the nodes that don't have a unique ID\n let tr = EditorState.create({\n doc: contentNode,\n }).tr\n // oxlint-disable-next-line no-restricted-syntax\n for (const { node, pos } of nodesWithoutId) {\n tr = tr.setNodeAttribute(pos, attributeName, generateID({ node, pos }))\n }\n\n // Return the updated document\n return tr.doc.toJSON()\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAQO;AAEP,mBAAgC;AAEhC,mBAAkC;AAClC,kBAA6B;;;ACVtB,SAAS,eAAe,OAAqB;AAClD,QAAM,OAAO,oBAAI,IAAI;AACrB,QAAM,aAAa,oBAAI,IAAS;AAEhC,QAAM,QAAQ,UAAQ;AACpB,QAAI,KAAK,IAAI,IAAI,GAAG;AAClB,iBAAW,IAAI,IAAI;AAAA,IACrB,OAAO;AACL,WAAK,IAAI,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,MAAM,KAAK,UAAU;AAC9B;;;ADwCA,IAAM,eAAe,CAAC,OAAiC,eAAqC;AAC1F,MAAI,UAAU,OAAO;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,EAAE,eAAe,QAAI,6BAAgB,UAAU;AAErD,SAAO,eACJ,IAAI,eAAa,UAAU,IAAI,EAC/B,OAAO,UAAQ,SAAS,SAAS,SAAS,MAAM;AACrD;AAEO,IAAM,WAAW,sBAAU,OAAwB;AAAA,EACxD,MAAM;AAAA;AAAA;AAAA,EAIN,UAAU;AAAA,EAEV,aAAa;AACX,WAAO;AAAA,MACL,eAAe;AAAA,MACf,OAAO,CAAC;AAAA,MACR,YAAY,UAAM,YAAAA,IAAO;AAAA,MACzB,mBAAmB;AAAA,MACnB,gBAAgB;AAAA,IAClB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,aAAa;AACX,WAAO;AAAA,MACL,0BAA0B;AAAA,IAC5B;AAAA,EACF;AAAA,EAEA,sBAAsB;AACpB,UAAM,QAAQ,aAAa,KAAK,QAAQ,OAAO,KAAK,UAAU;AAE9D,WAAO;AAAA,MACL;AAAA,QACE;AAAA,QACA,YAA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AAM,EAAE,OAAO,iBAAiB,eAAe,WAAW,IAAI,kBAAkB;AAChF,QAAM,QACJ,oBAAoB,QAChB,OAAO,KAAK,OAAO,KAAK,EAAE,OAAO,UAAQ,SAAS,SAAS,SAAS,MAAM,IAC1E;AACN,QAAM,cAAc,mBAAK,SAAS,QAAQ,GAAG;AAG7C,QAAM,qBAAiB,2BAAa,aAAa,UAAQ;AACvD,WAAO,CAAC,KAAK,MAAM,aAAa,KAAK,MAAM,SAAS,KAAK,KAAK,IAAI;AAAA,EACpE,CAAC;AAGD,MAAI,KAAK,0BAAY,OAAO;AAAA,IAC1B,KAAK;AAAA,EACP,CAAC,EAAE;AAEH,aAAW,EAAE,MAAM,IAAI,KAAK,gBAAgB;AAC1C,SAAK,GAAG,iBAAiB,KAAK,eAAe,WAAW,EAAE,MAAM,IAAI,CAAC,CAAC;AAAA,EACxE;AAGA,SAAO,GAAG,IAAI,OAAO;AACvB;;;AHzEA,IAAO,gBAAQ;","names":["uuidv4","_a","tr","attributeName","generateID","import_core","import_model","import_state"]}
|
package/dist/index.js
CHANGED
|
@@ -86,8 +86,12 @@ var UniqueID = Extension.create({
|
|
|
86
86
|
if (!this.options.updateDocument) {
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
|
-
const collaboration = this.editor.extensionManager.extensions.find(
|
|
90
|
-
|
|
89
|
+
const collaboration = this.editor.extensionManager.extensions.find(
|
|
90
|
+
(ext) => ext.name === "collaboration"
|
|
91
|
+
);
|
|
92
|
+
const collaborationCaret = this.editor.extensionManager.extensions.find(
|
|
93
|
+
(ext) => ext.name === "collaborationCaret"
|
|
94
|
+
);
|
|
91
95
|
const collabExtensions = [collaboration, collaborationCaret].filter(Boolean);
|
|
92
96
|
const collab = collabExtensions.find((ext) => {
|
|
93
97
|
var _a2;
|
|
@@ -127,8 +131,12 @@ var UniqueID = Extension.create({
|
|
|
127
131
|
return [];
|
|
128
132
|
}
|
|
129
133
|
const extensionStorage = this.storage;
|
|
130
|
-
const collaboration = this.editor.extensionManager.extensions.find(
|
|
131
|
-
|
|
134
|
+
const collaboration = this.editor.extensionManager.extensions.find(
|
|
135
|
+
(ext) => ext.name === "collaboration"
|
|
136
|
+
);
|
|
137
|
+
const collaborationCaret = this.editor.extensionManager.extensions.find(
|
|
138
|
+
(ext) => ext.name === "collaborationCaret"
|
|
139
|
+
);
|
|
132
140
|
const collabExtensions = [collaboration, collaborationCaret].filter(Boolean);
|
|
133
141
|
const collabWithProvider = collabExtensions.find((ext) => {
|
|
134
142
|
var _a;
|
|
@@ -320,7 +328,10 @@ function generateUniqueIds(doc, extensions) {
|
|
|
320
328
|
if (!uniqueIDExtension) {
|
|
321
329
|
throw new Error("UniqueID extension not found in the extensions array");
|
|
322
330
|
}
|
|
323
|
-
const schema = getSchema([
|
|
331
|
+
const schema = getSchema([
|
|
332
|
+
...extensions.filter((ext) => ext.name !== "uniqueID"),
|
|
333
|
+
uniqueIDExtension
|
|
334
|
+
]);
|
|
324
335
|
const { types: configuredTypes, attributeName, generateID } = uniqueIDExtension.options;
|
|
325
336
|
const types = configuredTypes === "all" ? Object.keys(schema.nodes).filter((type) => type !== "doc" && type !== "text") : configuredTypes;
|
|
326
337
|
const contentNode = Node.fromJSON(schema, doc);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/unique-id.ts","../src/helpers/findDuplicates.ts","../src/generate-unique-ids.ts","../src/index.ts"],"sourcesContent":["import {\n type Extensions,\n combineTransactionSteps,\n Extension,\n findChildren,\n findChildrenInRange,\n getChangedRanges,\n splitExtensions,\n} from '@tiptap/core'\nimport type { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Fragment, Slice } from '@tiptap/pm/model'\nimport type { Transaction } from '@tiptap/pm/state'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { v4 as uuidv4 } from 'uuid'\n\nimport { findDuplicates } from './helpers/findDuplicates.js'\n\nexport type UniqueIDGenerationContext = {\n node: ProseMirrorNode\n pos: number\n}\n\nexport interface UniqueIDOptions {\n /**\n * The name of the attribute to add the unique ID to.\n * @default \"id\"\n */\n attributeName: string\n /**\n * The types of nodes to add unique IDs to.\n * Use `\"all\"` to add IDs to every node type except `doc` and `text`.\n * @default []\n */\n types: string[] | 'all'\n /**\n * The function that generates the unique ID. By default, a UUID v4 is\n * generated. However, you can provide your own function to generate the\n * unique ID based on the node type and the position.\n */\n generateID: (ctx: UniqueIDGenerationContext) => any\n /**\n * Ignore some mutations, for example applied from other users through the collaboration plugin.\n *\n * @default null\n */\n filterTransaction: ((transaction: Transaction) => boolean) | null\n /**\n * Whether to update the document by adding unique IDs to the nodes. Set this\n * property to `false` if the document is in `readonly` mode, is immutable, or\n * you don't want it to be modified.\n *\n * @default true\n */\n updateDocument: boolean\n}\n\nconst resolveTypes = (types: UniqueIDOptions['types'], extensions: Extensions): string[] => {\n if (types !== 'all') {\n return types\n }\n\n const { nodeExtensions } = splitExtensions(extensions)\n\n return nodeExtensions.map(extension => extension.name).filter(type => type !== 'doc' && type !== 'text')\n}\n\nexport const UniqueID = Extension.create<UniqueIDOptions>({\n name: 'uniqueID',\n\n // we’ll set a very high priority to make sure this runs first\n // and is compatible with `appendTransaction` hooks of other extensions\n priority: 10000,\n\n addOptions() {\n return {\n attributeName: 'id',\n types: [],\n generateID: () => uuidv4(),\n filterTransaction: null,\n updateDocument: true,\n }\n },\n\n /**\n * Extension storage for coordination between `addProseMirrorPlugins` and `appendTransaction`.\n * `needsInitialIdGeneration` is set to `true` when the Collaboration extension is\n * detected but no provider is available in its options, deferring ID creation\n * to the first `y-sync$` transaction.\n */\n addStorage() {\n return {\n needsInitialIdGeneration: false,\n }\n },\n\n addGlobalAttributes() {\n const types = resolveTypes(this.options.types, this.extensions)\n\n return [\n {\n types,\n attributes: {\n [this.options.attributeName]: {\n default: null,\n parseHTML: element => element.getAttribute(`data-${this.options.attributeName}`),\n renderHTML: attributes => {\n if (!attributes[this.options.attributeName]) {\n return {}\n }\n\n return {\n [`data-${this.options.attributeName}`]: attributes[this.options.attributeName],\n }\n },\n },\n },\n },\n ]\n },\n\n // check initial content for missing ids\n onCreate() {\n if (!this.options.updateDocument) {\n return\n }\n\n const collaboration = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaboration')\n const collaborationCaret = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaborationCaret')\n\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collab = collabExtensions.find(ext => ext?.options?.provider)\n const provider = collab?.options?.provider\n\n const createIds = () => {\n const { view, state } = this.editor\n const { tr, doc } = state\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n const { attributeName, generateID } = this.options\n const nodesWithoutId = findChildren(doc, node => {\n return types.includes(node.type.name) && node.attrs[attributeName] === null\n })\n\n nodesWithoutId.forEach(({ node, pos }) => {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n })\n\n tr.setMeta('addToHistory', false)\n\n view.dispatch(tr)\n\n if (provider) {\n provider.off('synced', createIds)\n }\n }\n\n /**\n * We need to handle collaboration a bit different here\n * because we can't automatically add IDs when the provider is not yet synced\n * otherwise we end up with empty paragraphs\n */\n if (collaboration) {\n if (provider) {\n provider.on('synced', createIds)\n }\n // When collaboration is present but no provider is in extension options,\n // needsInitialIdGeneration was already set in addProseMirrorPlugins\n // (which runs synchronously during editor construction, before any\n // y-sync$ transaction can arrive).\n } else {\n return createIds()\n }\n },\n\n addProseMirrorPlugins() {\n if (!this.options.updateDocument) {\n return []\n }\n\n // Capture storage via closure so appendTransaction can access `needsInitialIdGeneration`.\n // `extensionManager.extensions` returns different objects than `this` due to\n // Tiptap's child-extension flattening, so we capture the reference directly.\n const extensionStorage = this.storage\n\n // Detect collaboration early — before any y-sync$ transaction can arrive.\n // onCreate runs on a deferred setTimeout(0), so a y-sync$ transaction could\n // be applied before it. Setting the flag here (synchronous during editor\n // construction) ensures the first y-sync$ is always handled.\n const collaboration = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaboration')\n const collaborationCaret = this.editor.extensionManager.extensions.find(ext => ext.name === 'collaborationCaret')\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collabWithProvider = collabExtensions.find(ext => ext?.options?.provider)\n\n if (collaboration && !collabWithProvider) {\n extensionStorage.needsInitialIdGeneration = true\n }\n\n let dragSourceElement: Element | null = null\n let transformPasted = false\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n\n return [\n new Plugin({\n key: new PluginKey('uniqueID'),\n\n appendTransaction: (transactions, oldState, newState) => {\n const hasDocChanges =\n transactions.some(transaction => transaction.docChanged) && !oldState.doc.eq(newState.doc)\n const filterTransactions =\n this.options.filterTransaction && transactions.some(tr => !this.options.filterTransaction?.(tr))\n\n const isCollabTransaction = transactions.find(tr => tr.getMeta('y-sync$'))\n\n if (isCollabTransaction) {\n if (extensionStorage.needsInitialIdGeneration) {\n extensionStorage.needsInitialIdGeneration = false\n\n // Run full-document ID creation after the first Yjs sync.\n // Use a seen-set so only the second+ occurrence of a duplicated\n // ID is regenerated, preserving one existing ID per value.\n const { tr } = newState\n const { attributeName, generateID } = this.options\n const allNodes = findChildren(newState.doc, node => types.includes(node.type.name))\n const seen = new Set<string>()\n\n allNodes.forEach(({ node, pos }) => {\n const currentId = node.attrs[attributeName]\n\n if (currentId === null || seen.has(currentId)) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n } else {\n seen.add(currentId)\n }\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // Restore stored marks since setNodeMarkup resets them\n tr.setStoredMarks(newState.tr.storedMarks)\n // Mark this transaction as coming from UniqueID to prevent\n // infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n tr.setMeta('addToHistory', false)\n return tr\n }\n\n return\n }\n\n if (!hasDocChanges || filterTransactions) {\n return\n }\n\n const { tr } = newState\n\n const { attributeName, generateID } = this.options\n const transform = combineTransactionSteps(oldState.doc, transactions as Transaction[])\n const { mapping } = transform\n\n // get changed ranges based on the old state\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ newRange }) => {\n const newNodes = findChildrenInRange(newState.doc, newRange, node => {\n return types.includes(node.type.name)\n })\n\n const newIds = newNodes.map(({ node }) => node.attrs[attributeName]).filter(id => id !== null)\n\n newNodes.forEach(({ node, pos }, i) => {\n // instead of checking `node.attrs[attributeName]` directly\n // we look at the current state of the node within `tr.doc`.\n // this helps to prevent adding new ids to the same node\n // if the node changed multiple times within one transaction\n const id = tr.doc.nodeAt(pos)?.attrs[attributeName]\n\n if (id === null) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n\n return\n }\n\n const nextNode = newNodes[i + 1]\n\n if (nextNode && node.content.size === 0) {\n const nextNodeInTr = tr.doc.nodeAt(nextNode.pos)\n if (nextNodeInTr?.attrs[attributeName] && nextNodeInTr.attrs[attributeName] !== id) {\n return\n }\n\n tr.setNodeMarkup(nextNode.pos, undefined, {\n ...nextNode.node.attrs,\n [attributeName]: id,\n })\n newIds[i + 1] = id\n\n const generatedId = generateID({ node, pos })\n\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generatedId,\n })\n newIds[i] = generatedId\n\n return tr\n }\n\n const duplicatedNewIds = findDuplicates(newIds)\n\n // check if the node doesn’t exist in the old state\n const { deleted } = mapping.invert().mapResult(pos)\n\n const newNode = deleted && duplicatedNewIds.includes(id)\n\n if (newNode) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n }\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // `tr.setNodeMarkup` resets the stored marks\n // so we'll restore them if they exist\n tr.setStoredMarks(newState.tr.storedMarks)\n\n // Mark this transaction as coming from UniqueID\n // to prevent infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n\n return tr\n },\n\n // we register a global drag handler to track the current drag source element\n view(view) {\n const handleDragstart = (event: DragEvent) => {\n dragSourceElement = view.dom.parentElement?.contains(event.target as Element)\n ? view.dom.parentElement\n : null\n }\n\n window.addEventListener('dragstart', handleDragstart)\n\n return {\n destroy() {\n window.removeEventListener('dragstart', handleDragstart)\n },\n }\n },\n\n props: {\n // `handleDOMEvents` is called before `transformPasted`\n // so we can do some checks before\n handleDOMEvents: {\n // only create new ids for dropped content\n // or dropped content while holding `alt`\n // or content is dragged from another editor\n drop: (view, event) => {\n if (\n dragSourceElement !== view.dom.parentElement ||\n event.dataTransfer?.effectAllowed === 'copyMove' ||\n event.dataTransfer?.effectAllowed === 'copy'\n ) {\n dragSourceElement = null\n transformPasted = true\n }\n\n return false\n },\n // always create new ids on pasted content\n paste: () => {\n transformPasted = true\n\n return false\n },\n },\n\n // we’ll remove ids for every pasted node\n // so we can create a new one within `appendTransaction`\n transformPasted: slice => {\n if (!transformPasted) {\n return slice\n }\n\n const { attributeName } = this.options\n const removeId = (fragment: Fragment): Fragment => {\n const list: ProseMirrorNode[] = []\n\n fragment.forEach(node => {\n // don’t touch text nodes\n if (node.isText) {\n list.push(node)\n\n return\n }\n\n // check for any other child nodes\n if (!types.includes(node.type.name)) {\n list.push(node.copy(removeId(node.content)))\n\n return\n }\n\n // remove id\n const nodeWithoutId = node.type.create(\n {\n ...node.attrs,\n [attributeName]: null,\n },\n removeId(node.content),\n node.marks,\n )\n\n list.push(nodeWithoutId)\n })\n\n return Fragment.from(list)\n }\n\n // reset check\n transformPasted = false\n\n return new Slice(removeId(slice.content), slice.openStart, slice.openEnd)\n },\n },\n }),\n ]\n },\n})\n","/**\n * Returns a list of duplicated items within an array.\n */\nexport function findDuplicates(items: any[]): any[] {\n const seen = new Set()\n const duplicates = new Set<any>()\n\n items.forEach(item => {\n if (seen.has(item)) {\n duplicates.add(item)\n } else {\n seen.add(item)\n }\n })\n\n return Array.from(duplicates)\n}\n","import type { Extensions, JSONContent } from '@tiptap/core'\nimport { findChildren, getSchema } from '@tiptap/core'\nimport { Node } from '@tiptap/pm/model'\nimport { EditorState } from '@tiptap/pm/state'\n\nimport type { UniqueID } from './unique-id.js'\n\n/**\n * Creates a new document with unique IDs added to the nodes. Does the same\n * thing as the UniqueID extension, but without the need to create an `Editor`\n * instance. This lets you add unique IDs to the document in the server.\n *\n * When you call it, include the `UniqueID` extension in the `extensions` array.\n * The configuration from the `UniqueID` extension will be picked up\n * automatically, including its configuration options like `types` and\n * `attributeName`.\n *\n * @see `UniqueID` extension for more information.\n *\n * @throws {Error} If the `UniqueID` extension is not found in the extensions array.\n *\n * @example\n * const doc = {\n * type: 'doc',\n * content: [\n * { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }] }\n * ]\n * }\n * const newDoc = addUniqueIds(doc, [StarterKit, UniqueID.configure({ types: ['paragraph', 'heading'] })])\n * console.log(newDoc)\n * // Result:\n * // {\n * // type: 'doc',\n * // content: [\n * // { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }], id: '123' }\n * // ]\n * // }\n *\n * @param doc - A Tiptap JSON document to add unique IDs to.\n * @param extensions - The extensions to use. Must include the `UniqueID` extension.\n * @returns The updated Tiptap JSON document, with the unique IDs added to the nodes.\n */\nexport function generateUniqueIds(doc: JSONContent, extensions: Extensions): JSONContent {\n // Find the UniqueID extension in the extensions array. If it's not found, throw an error.\n const uniqueIDExtension = extensions.find(ext => ext.name === 'uniqueID') as typeof UniqueID | undefined\n if (!uniqueIDExtension) {\n throw new Error('UniqueID extension not found in the extensions array')\n }\n // Convert the JSON content to a ProseMirror node\n const schema = getSchema([...extensions.filter(ext => ext.name !== 'uniqueID'), uniqueIDExtension])\n const { types: configuredTypes, attributeName, generateID } = uniqueIDExtension.options\n const types =\n configuredTypes === 'all'\n ? Object.keys(schema.nodes).filter(type => type !== 'doc' && type !== 'text')\n : configuredTypes\n const contentNode = Node.fromJSON(schema, doc)\n\n // Find nodes that don't have a unique ID\n const nodesWithoutId = findChildren(contentNode, node => {\n return !node.attrs[attributeName] && types.includes(node.type.name)\n })\n\n // Edit the document to add unique IDs to the nodes that don't have a unique ID\n let tr = EditorState.create({\n doc: contentNode,\n }).tr\n // eslint-disable-next-line no-restricted-syntax\n for (const { node, pos } of nodesWithoutId) {\n tr = tr.setNodeAttribute(pos, attributeName, generateID({ node, pos }))\n }\n\n // Return the updated document\n return tr.doc.toJSON()\n}\n","import { UniqueID } from './unique-id.js'\n\nexport * from './generate-unique-ids.js'\nexport * from './unique-id.js'\n\nexport default UniqueID\n"],"mappings":";AAAA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,UAAU,aAAa;AAEhC,SAAS,QAAQ,iBAAiB;AAClC,SAAS,MAAM,cAAc;;;ACVtB,SAAS,eAAe,OAAqB;AAClD,QAAM,OAAO,oBAAI,IAAI;AACrB,QAAM,aAAa,oBAAI,IAAS;AAEhC,QAAM,QAAQ,UAAQ;AACpB,QAAI,KAAK,IAAI,IAAI,GAAG;AAClB,iBAAW,IAAI,IAAI;AAAA,IACrB,OAAO;AACL,WAAK,IAAI,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,MAAM,KAAK,UAAU;AAC9B;;;ADwCA,IAAM,eAAe,CAAC,OAAiC,eAAqC;AAC1F,MAAI,UAAU,OAAO;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,EAAE,eAAe,IAAI,gBAAgB,UAAU;AAErD,SAAO,eAAe,IAAI,eAAa,UAAU,IAAI,EAAE,OAAO,UAAQ,SAAS,SAAS,SAAS,MAAM;AACzG;AAEO,IAAM,WAAW,UAAU,OAAwB;AAAA,EACxD,MAAM;AAAA;AAAA;AAAA,EAIN,UAAU;AAAA,EAEV,aAAa;AACX,WAAO;AAAA,MACL,eAAe;AAAA,MACf,OAAO,CAAC;AAAA,MACR,YAAY,MAAM,OAAO;AAAA,MACzB,mBAAmB;AAAA,MAC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,oBAAoB,WAAW,KAAK,SAAO,IAAI,SAAS,UAAU;AACxE,MAAI,CAAC,mBAAmB;AACtB,UAAM,IAAI,MAAM,sDAAsD;AAAA,EACxE;AAEA,QAAM,SAAS,UAAU,CAAC,GAAG,WAAW,OAAO,SAAO,IAAI,SAAS,UAAU,GAAG,iBAAiB,CAAC;AAClG,QAAM,EAAE,OAAO,iBAAiB,eAAe,WAAW,IAAI,kBAAkB;AAChF,QAAM,QACJ,oBAAoB,QAChB,OAAO,KAAK,OAAO,KAAK,EAAE,OAAO,UAAQ,SAAS,SAAS,SAAS,MAAM,IAC1E;AACN,QAAM,cAAc,KAAK,SAAS,QAAQ,GAAG;AAG7C,QAAM,iBAAiBA,cAAa,aAAa,UAAQ;AACvD,WAAO,CAAC,KAAK,MAAM,aAAa,KAAK,MAAM,SAAS,KAAK,KAAK,IAAI;AAAA,EACpE,CAAC;AAGD,MAAI,KAAK,YAAY,OAAO;AAAA,IAC1B,KAAK;AAAA,EACP,CAAC,EAAE;AAEH,aAAW,EAAE,MAAM,IAAI,KAAK,gBAAgB;AAC1C,SAAK,GAAG,iBAAiB,KAAK,eAAe,WAAW,EAAE,MAAM,IAAI,CAAC,CAAC;AAAA,EACxE;AAGA,SAAO,GAAG,IAAI,OAAO;AACvB;;;ACpEA,IAAO,gBAAQ;","names":["_a","tr","attributeName","generateID","findChildren"]}
|
|
1
|
+
{"version":3,"sources":["../src/unique-id.ts","../src/helpers/findDuplicates.ts","../src/generate-unique-ids.ts","../src/index.ts"],"sourcesContent":["import {\n type Extensions,\n combineTransactionSteps,\n Extension,\n findChildren,\n findChildrenInRange,\n getChangedRanges,\n splitExtensions,\n} from '@tiptap/core'\nimport type { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Fragment, Slice } from '@tiptap/pm/model'\nimport type { Transaction } from '@tiptap/pm/state'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { v4 as uuidv4 } from 'uuid'\n\nimport { findDuplicates } from './helpers/findDuplicates.js'\n\nexport type UniqueIDGenerationContext = {\n node: ProseMirrorNode\n pos: number\n}\n\nexport interface UniqueIDOptions {\n /**\n * The name of the attribute to add the unique ID to.\n * @default \"id\"\n */\n attributeName: string\n /**\n * The types of nodes to add unique IDs to.\n * Use `\"all\"` to add IDs to every node type except `doc` and `text`.\n * @default []\n */\n types: string[] | 'all'\n /**\n * The function that generates the unique ID. By default, a UUID v4 is\n * generated. However, you can provide your own function to generate the\n * unique ID based on the node type and the position.\n */\n generateID: (ctx: UniqueIDGenerationContext) => any\n /**\n * Ignore some mutations, for example applied from other users through the collaboration plugin.\n *\n * @default null\n */\n filterTransaction: ((transaction: Transaction) => boolean) | null\n /**\n * Whether to update the document by adding unique IDs to the nodes. Set this\n * property to `false` if the document is in `readonly` mode, is immutable, or\n * you don't want it to be modified.\n *\n * @default true\n */\n updateDocument: boolean\n}\n\nconst resolveTypes = (types: UniqueIDOptions['types'], extensions: Extensions): string[] => {\n if (types !== 'all') {\n return types\n }\n\n const { nodeExtensions } = splitExtensions(extensions)\n\n return nodeExtensions\n .map(extension => extension.name)\n .filter(type => type !== 'doc' && type !== 'text')\n}\n\nexport const UniqueID = Extension.create<UniqueIDOptions>({\n name: 'uniqueID',\n\n // we’ll set a very high priority to make sure this runs first\n // and is compatible with `appendTransaction` hooks of other extensions\n priority: 10000,\n\n addOptions() {\n return {\n attributeName: 'id',\n types: [],\n generateID: () => uuidv4(),\n filterTransaction: null,\n updateDocument: true,\n }\n },\n\n /**\n * Extension storage for coordination between `addProseMirrorPlugins` and `appendTransaction`.\n * `needsInitialIdGeneration` is set to `true` when the Collaboration extension is\n * detected but no provider is available in its options, deferring ID creation\n * to the first `y-sync$` transaction.\n */\n addStorage() {\n return {\n needsInitialIdGeneration: false,\n }\n },\n\n addGlobalAttributes() {\n const types = resolveTypes(this.options.types, this.extensions)\n\n return [\n {\n types,\n attributes: {\n [this.options.attributeName]: {\n default: null,\n parseHTML: element => element.getAttribute(`data-${this.options.attributeName}`),\n renderHTML: attributes => {\n if (!attributes[this.options.attributeName]) {\n return {}\n }\n\n return {\n [`data-${this.options.attributeName}`]: attributes[this.options.attributeName],\n }\n },\n },\n },\n },\n ]\n },\n\n // check initial content for missing ids\n onCreate() {\n if (!this.options.updateDocument) {\n return\n }\n\n const collaboration = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaboration',\n )\n const collaborationCaret = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaborationCaret',\n )\n\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collab = collabExtensions.find(ext => ext?.options?.provider)\n const provider = collab?.options?.provider\n\n const createIds = () => {\n const { view, state } = this.editor\n const { tr, doc } = state\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n const { attributeName, generateID } = this.options\n const nodesWithoutId = findChildren(doc, node => {\n return types.includes(node.type.name) && node.attrs[attributeName] === null\n })\n\n nodesWithoutId.forEach(({ node, pos }) => {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n })\n\n tr.setMeta('addToHistory', false)\n\n view.dispatch(tr)\n\n if (provider) {\n provider.off('synced', createIds)\n }\n }\n\n /**\n * We need to handle collaboration a bit different here\n * because we can't automatically add IDs when the provider is not yet synced\n * otherwise we end up with empty paragraphs\n */\n if (collaboration) {\n if (provider) {\n provider.on('synced', createIds)\n }\n // When collaboration is present but no provider is in extension options,\n // needsInitialIdGeneration was already set in addProseMirrorPlugins\n // (which runs synchronously during editor construction, before any\n // y-sync$ transaction can arrive).\n } else {\n return createIds()\n }\n },\n\n addProseMirrorPlugins() {\n if (!this.options.updateDocument) {\n return []\n }\n\n // Capture storage via closure so appendTransaction can access `needsInitialIdGeneration`.\n // `extensionManager.extensions` returns different objects than `this` due to\n // Tiptap's child-extension flattening, so we capture the reference directly.\n const extensionStorage = this.storage\n\n // Detect collaboration early — before any y-sync$ transaction can arrive.\n // onCreate runs on a deferred setTimeout(0), so a y-sync$ transaction could\n // be applied before it. Setting the flag here (synchronous during editor\n // construction) ensures the first y-sync$ is always handled.\n const collaboration = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaboration',\n )\n const collaborationCaret = this.editor.extensionManager.extensions.find(\n ext => ext.name === 'collaborationCaret',\n )\n const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)\n const collabWithProvider = collabExtensions.find(ext => ext?.options?.provider)\n\n if (collaboration && !collabWithProvider) {\n extensionStorage.needsInitialIdGeneration = true\n }\n\n let dragSourceElement: Element | null = null\n let transformPasted = false\n const types = resolveTypes(this.options.types, this.editor.extensionManager.extensions)\n\n return [\n new Plugin({\n key: new PluginKey('uniqueID'),\n\n appendTransaction: (transactions, oldState, newState) => {\n const hasDocChanges =\n transactions.some(transaction => transaction.docChanged) &&\n !oldState.doc.eq(newState.doc)\n const filterTransactions =\n this.options.filterTransaction &&\n transactions.some(tr => !this.options.filterTransaction?.(tr))\n\n const isCollabTransaction = transactions.find(tr => tr.getMeta('y-sync$'))\n\n if (isCollabTransaction) {\n if (extensionStorage.needsInitialIdGeneration) {\n extensionStorage.needsInitialIdGeneration = false\n\n // Run full-document ID creation after the first Yjs sync.\n // Use a seen-set so only the second+ occurrence of a duplicated\n // ID is regenerated, preserving one existing ID per value.\n const { tr } = newState\n const { attributeName, generateID } = this.options\n const allNodes = findChildren(newState.doc, node => types.includes(node.type.name))\n const seen = new Set<string>()\n\n allNodes.forEach(({ node, pos }) => {\n const currentId = node.attrs[attributeName]\n\n if (currentId === null || seen.has(currentId)) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n } else {\n seen.add(currentId)\n }\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // Restore stored marks since setNodeMarkup resets them\n tr.setStoredMarks(newState.tr.storedMarks)\n // Mark this transaction as coming from UniqueID to prevent\n // infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n tr.setMeta('addToHistory', false)\n return tr\n }\n\n return\n }\n\n if (!hasDocChanges || filterTransactions) {\n return\n }\n\n const { tr } = newState\n\n const { attributeName, generateID } = this.options\n const transform = combineTransactionSteps(oldState.doc, transactions as Transaction[])\n const { mapping } = transform\n\n // get changed ranges based on the old state\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ newRange }) => {\n const newNodes = findChildrenInRange(newState.doc, newRange, node => {\n return types.includes(node.type.name)\n })\n\n const newIds = newNodes\n .map(({ node }) => node.attrs[attributeName])\n .filter(id => id !== null)\n\n newNodes.forEach(({ node, pos }, i) => {\n // instead of checking `node.attrs[attributeName]` directly\n // we look at the current state of the node within `tr.doc`.\n // this helps to prevent adding new ids to the same node\n // if the node changed multiple times within one transaction\n const id = tr.doc.nodeAt(pos)?.attrs[attributeName]\n\n if (id === null) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n\n return\n }\n\n const nextNode = newNodes[i + 1]\n\n if (nextNode && node.content.size === 0) {\n const nextNodeInTr = tr.doc.nodeAt(nextNode.pos)\n if (\n nextNodeInTr?.attrs[attributeName] &&\n nextNodeInTr.attrs[attributeName] !== id\n ) {\n return\n }\n\n tr.setNodeMarkup(nextNode.pos, undefined, {\n ...nextNode.node.attrs,\n [attributeName]: id,\n })\n newIds[i + 1] = id\n\n const generatedId = generateID({ node, pos })\n\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generatedId,\n })\n newIds[i] = generatedId\n\n return tr\n }\n\n const duplicatedNewIds = findDuplicates(newIds)\n\n // check if the node doesn’t exist in the old state\n const { deleted } = mapping.invert().mapResult(pos)\n\n const newNode = deleted && duplicatedNewIds.includes(id)\n\n if (newNode) {\n tr.setNodeMarkup(pos, undefined, {\n ...node.attrs,\n [attributeName]: generateID({ node, pos }),\n })\n }\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n // `tr.setNodeMarkup` resets the stored marks\n // so we'll restore them if they exist\n tr.setStoredMarks(newState.tr.storedMarks)\n\n // Mark this transaction as coming from UniqueID\n // to prevent infinite loops with other extensions (e.g., TrailingNode)\n tr.setMeta('__uniqueIDTransaction', true)\n\n return tr\n },\n\n // we register a global drag handler to track the current drag source element\n view(view) {\n const handleDragstart = (event: DragEvent) => {\n dragSourceElement = view.dom.parentElement?.contains(event.target as Element)\n ? view.dom.parentElement\n : null\n }\n\n window.addEventListener('dragstart', handleDragstart)\n\n return {\n destroy() {\n window.removeEventListener('dragstart', handleDragstart)\n },\n }\n },\n\n props: {\n // `handleDOMEvents` is called before `transformPasted`\n // so we can do some checks before\n handleDOMEvents: {\n // only create new ids for dropped content\n // or dropped content while holding `alt`\n // or content is dragged from another editor\n drop: (view, event) => {\n if (\n dragSourceElement !== view.dom.parentElement ||\n event.dataTransfer?.effectAllowed === 'copyMove' ||\n event.dataTransfer?.effectAllowed === 'copy'\n ) {\n dragSourceElement = null\n transformPasted = true\n }\n\n return false\n },\n // always create new ids on pasted content\n paste: () => {\n transformPasted = true\n\n return false\n },\n },\n\n // we’ll remove ids for every pasted node\n // so we can create a new one within `appendTransaction`\n transformPasted: slice => {\n if (!transformPasted) {\n return slice\n }\n\n const { attributeName } = this.options\n const removeId = (fragment: Fragment): Fragment => {\n const list: ProseMirrorNode[] = []\n\n fragment.forEach(node => {\n // don’t touch text nodes\n if (node.isText) {\n list.push(node)\n\n return\n }\n\n // check for any other child nodes\n if (!types.includes(node.type.name)) {\n list.push(node.copy(removeId(node.content)))\n\n return\n }\n\n // remove id\n const nodeWithoutId = node.type.create(\n {\n ...node.attrs,\n [attributeName]: null,\n },\n removeId(node.content),\n node.marks,\n )\n\n list.push(nodeWithoutId)\n })\n\n return Fragment.from(list)\n }\n\n // reset check\n transformPasted = false\n\n return new Slice(removeId(slice.content), slice.openStart, slice.openEnd)\n },\n },\n }),\n ]\n },\n})\n","/**\n * Returns a list of duplicated items within an array.\n */\nexport function findDuplicates(items: any[]): any[] {\n const seen = new Set()\n const duplicates = new Set<any>()\n\n items.forEach(item => {\n if (seen.has(item)) {\n duplicates.add(item)\n } else {\n seen.add(item)\n }\n })\n\n return Array.from(duplicates)\n}\n","import type { Extensions, JSONContent } from '@tiptap/core'\nimport { findChildren, getSchema } from '@tiptap/core'\nimport { Node } from '@tiptap/pm/model'\nimport { EditorState } from '@tiptap/pm/state'\n\nimport type { UniqueID } from './unique-id.js'\n\n/**\n * Creates a new document with unique IDs added to the nodes. Does the same\n * thing as the UniqueID extension, but without the need to create an `Editor`\n * instance. This lets you add unique IDs to the document in the server.\n *\n * When you call it, include the `UniqueID` extension in the `extensions` array.\n * The configuration from the `UniqueID` extension will be picked up\n * automatically, including its configuration options like `types` and\n * `attributeName`.\n *\n * @see `UniqueID` extension for more information.\n *\n * @throws {Error} If the `UniqueID` extension is not found in the extensions array.\n *\n * @example\n * const doc = {\n * type: 'doc',\n * content: [\n * { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }] }\n * ]\n * }\n * const newDoc = addUniqueIds(doc, [StarterKit, UniqueID.configure({ types: ['paragraph', 'heading'] })])\n * console.log(newDoc)\n * // Result:\n * // {\n * // type: 'doc',\n * // content: [\n * // { type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }], id: '123' }\n * // ]\n * // }\n *\n * @param doc - A Tiptap JSON document to add unique IDs to.\n * @param extensions - The extensions to use. Must include the `UniqueID` extension.\n * @returns The updated Tiptap JSON document, with the unique IDs added to the nodes.\n */\nexport function generateUniqueIds(doc: JSONContent, extensions: Extensions): JSONContent {\n // Find the UniqueID extension in the extensions array. If it's not found, throw an error.\n const uniqueIDExtension = extensions.find(ext => ext.name === 'uniqueID') as\n | typeof UniqueID\n | undefined\n if (!uniqueIDExtension) {\n throw new Error('UniqueID extension not found in the extensions array')\n }\n // Convert the JSON content to a ProseMirror node\n const schema = getSchema([\n ...extensions.filter(ext => ext.name !== 'uniqueID'),\n uniqueIDExtension,\n ])\n const { types: configuredTypes, attributeName, generateID } = uniqueIDExtension.options\n const types =\n configuredTypes === 'all'\n ? Object.keys(schema.nodes).filter(type => type !== 'doc' && type !== 'text')\n : configuredTypes\n const contentNode = Node.fromJSON(schema, doc)\n\n // Find nodes that don't have a unique ID\n const nodesWithoutId = findChildren(contentNode, node => {\n return !node.attrs[attributeName] && types.includes(node.type.name)\n })\n\n // Edit the document to add unique IDs to the nodes that don't have a unique ID\n let tr = EditorState.create({\n doc: contentNode,\n }).tr\n // oxlint-disable-next-line no-restricted-syntax\n for (const { node, pos } of nodesWithoutId) {\n tr = tr.setNodeAttribute(pos, attributeName, generateID({ node, pos }))\n }\n\n // Return the updated document\n return tr.doc.toJSON()\n}\n","import { UniqueID } from './unique-id.js'\n\nexport * from './generate-unique-ids.js'\nexport * from './unique-id.js'\n\nexport default UniqueID\n"],"mappings":";AAAA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,UAAU,aAAa;AAEhC,SAAS,QAAQ,iBAAiB;AAClC,SAAS,MAAM,cAAc;;;ACVtB,SAAS,eAAe,OAAqB;AAClD,QAAM,OAAO,oBAAI,IAAI;AACrB,QAAM,aAAa,oBAAI,IAAS;AAEhC,QAAM,QAAQ,UAAQ;AACpB,QAAI,KAAK,IAAI,IAAI,GAAG;AAClB,iBAAW,IAAI,IAAI;AAAA,IACrB,OAAO;AACL,WAAK,IAAI,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,MAAM,KAAK,UAAU;AAC9B;;;ADwCA,IAAM,eAAe,CAAC,OAAiC,eAAqC;AAC1F,MAAI,UAAU,OAAO;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,EAAE,eAAe,IAAI,gBAAgB,UAAU;AAErD,SAAO,eACJ,IAAI,eAAa,UAAU,IAAI,EAC/B,OAAO,UAAQ,SAAS,SAAS,SAAS,MAAM;AACrD;AAEO,IAAM,WAAW,UAAU,OAAwB;AAAA,EACxD,MAAM;AAAA;AAAA;AAAA,EAIN,UAAU;AAAA,EAEV,a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ACH;AAAA,EACF;AACF,CAAC;;;AE3cD,SAAS,gBAAAG,eAAc,iBAAiB;AACxC,SAAS,YAAY;AACrB,SAAS,mBAAmB;AAuCrB,SAAS,kBAAkB,KAAkB,YAAqC;AAEvF,QAAM,oBAAoB,WAAW,KAAK,SAAO,IAAI,SAAS,UAAU;AAGxE,MAAI,CAAC,mBAAmB;AACtB,UAAM,IAAI,MAAM,sDAAsD;AAAA,EACxE;AAEA,QAAM,SAAS,UAAU;AAAA,IACvB,GAAG,WAAW,OAAO,SAAO,IAAI,SAAS,UAAU;AAAA,IACnD;AAAA,EACF,CAAC;AACD,QAAM,EAAE,OAAO,iBAAiB,eAAe,WAAW,IAAI,kBAAkB;AAChF,QAAM,QACJ,oBAAoB,QAChB,OAAO,KAAK,OAAO,KAAK,EAAE,OAAO,UAAQ,SAAS,SAAS,SAAS,MAAM,IAC1E;AACN,QAAM,cAAc,KAAK,SAAS,QAAQ,GAAG;AAG7C,QAAM,iBAAiBA,cAAa,aAAa,UAAQ;AACvD,WAAO,CAAC,KAAK,MAAM,aAAa,KAAK,MAAM,SAAS,KAAK,KAAK,IAAI;AAAA,EACpE,CAAC;AAGD,MAAI,KAAK,YAAY,OAAO;AAAA,IAC1B,KAAK;AAAA,EACP,CAAC,EAAE;AAEH,aAAW,EAAE,MAAM,IAAI,KAAK,gBAAgB;AAC1C,SAAK,GAAG,iBAAiB,KAAK,eAAe,WAAW,EAAE,MAAM,IAAI,CAAC,CAAC;AAAA,EACxE;AAGA,SAAO,GAAG,IAAI,OAAO;AACvB;;;ACzEA,IAAO,gBAAQ;","names":["_a","tr","attributeName","generateID","findChildren"]}
|
package/package.json
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/extension-unique-id",
|
|
3
|
+
"version": "3.24.0",
|
|
3
4
|
"description": "unique id extension for tiptap",
|
|
4
|
-
"version": "3.23.5",
|
|
5
|
-
"homepage": "https://tiptap.dev/api/extensions/unique-id",
|
|
6
5
|
"keywords": [
|
|
7
6
|
"tiptap",
|
|
8
7
|
"tiptap extension"
|
|
9
8
|
],
|
|
9
|
+
"homepage": "https://tiptap.dev/api/extensions/unique-id",
|
|
10
10
|
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/ueberdosis/tiptap",
|
|
14
|
+
"directory": "packages/extension-unique-id"
|
|
15
|
+
},
|
|
11
16
|
"funding": {
|
|
12
17
|
"type": "github",
|
|
13
18
|
"url": "https://github.com/sponsors/ueberdosis"
|
|
14
19
|
},
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
15
24
|
"type": "module",
|
|
25
|
+
"main": "dist/index.cjs",
|
|
26
|
+
"module": "dist/index.js",
|
|
27
|
+
"types": "dist/index.d.ts",
|
|
16
28
|
"exports": {
|
|
17
29
|
".": {
|
|
18
30
|
"types": {
|
|
@@ -23,31 +35,18 @@
|
|
|
23
35
|
"require": "./dist/index.cjs"
|
|
24
36
|
}
|
|
25
37
|
},
|
|
26
|
-
"main": "dist/index.cjs",
|
|
27
|
-
"module": "dist/index.js",
|
|
28
|
-
"types": "dist/index.d.ts",
|
|
29
|
-
"files": [
|
|
30
|
-
"src",
|
|
31
|
-
"dist"
|
|
32
|
-
],
|
|
33
|
-
"peerDependencies": {
|
|
34
|
-
"@tiptap/core": "3.23.5",
|
|
35
|
-
"@tiptap/pm": "3.23.5"
|
|
36
|
-
},
|
|
37
38
|
"dependencies": {
|
|
38
39
|
"uuid": "^14.0.0"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@tiptap/core": "^3.
|
|
42
|
-
"@tiptap/pm": "^3.
|
|
42
|
+
"@tiptap/core": "^3.24.0",
|
|
43
|
+
"@tiptap/pm": "^3.24.0"
|
|
43
44
|
},
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"directory": "packages/extension-unique-id"
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@tiptap/core": "3.24.0",
|
|
47
|
+
"@tiptap/pm": "3.24.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
|
-
"build": "tsup"
|
|
51
|
-
"lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
|
|
50
|
+
"build": "tsup"
|
|
52
51
|
}
|
|
53
52
|
}
|
|
@@ -42,12 +42,17 @@ import type { UniqueID } from './unique-id.js'
|
|
|
42
42
|
*/
|
|
43
43
|
export function generateUniqueIds(doc: JSONContent, extensions: Extensions): JSONContent {
|
|
44
44
|
// Find the UniqueID extension in the extensions array. If it's not found, throw an error.
|
|
45
|
-
const uniqueIDExtension = extensions.find(ext => ext.name === 'uniqueID') as
|
|
45
|
+
const uniqueIDExtension = extensions.find(ext => ext.name === 'uniqueID') as
|
|
46
|
+
| typeof UniqueID
|
|
47
|
+
| undefined
|
|
46
48
|
if (!uniqueIDExtension) {
|
|
47
49
|
throw new Error('UniqueID extension not found in the extensions array')
|
|
48
50
|
}
|
|
49
51
|
// Convert the JSON content to a ProseMirror node
|
|
50
|
-
const schema = getSchema([
|
|
52
|
+
const schema = getSchema([
|
|
53
|
+
...extensions.filter(ext => ext.name !== 'uniqueID'),
|
|
54
|
+
uniqueIDExtension,
|
|
55
|
+
])
|
|
51
56
|
const { types: configuredTypes, attributeName, generateID } = uniqueIDExtension.options
|
|
52
57
|
const types =
|
|
53
58
|
configuredTypes === 'all'
|
|
@@ -64,7 +69,7 @@ export function generateUniqueIds(doc: JSONContent, extensions: Extensions): JSO
|
|
|
64
69
|
let tr = EditorState.create({
|
|
65
70
|
doc: contentNode,
|
|
66
71
|
}).tr
|
|
67
|
-
//
|
|
72
|
+
// oxlint-disable-next-line no-restricted-syntax
|
|
68
73
|
for (const { node, pos } of nodesWithoutId) {
|
|
69
74
|
tr = tr.setNodeAttribute(pos, attributeName, generateID({ node, pos }))
|
|
70
75
|
}
|
package/src/unique-id.ts
CHANGED
|
@@ -61,7 +61,9 @@ const resolveTypes = (types: UniqueIDOptions['types'], extensions: Extensions):
|
|
|
61
61
|
|
|
62
62
|
const { nodeExtensions } = splitExtensions(extensions)
|
|
63
63
|
|
|
64
|
-
return nodeExtensions
|
|
64
|
+
return nodeExtensions
|
|
65
|
+
.map(extension => extension.name)
|
|
66
|
+
.filter(type => type !== 'doc' && type !== 'text')
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
export const UniqueID = Extension.create<UniqueIDOptions>({
|
|
@@ -124,8 +126,12 @@ export const UniqueID = Extension.create<UniqueIDOptions>({
|
|
|
124
126
|
return
|
|
125
127
|
}
|
|
126
128
|
|
|
127
|
-
const collaboration = this.editor.extensionManager.extensions.find(
|
|
128
|
-
|
|
129
|
+
const collaboration = this.editor.extensionManager.extensions.find(
|
|
130
|
+
ext => ext.name === 'collaboration',
|
|
131
|
+
)
|
|
132
|
+
const collaborationCaret = this.editor.extensionManager.extensions.find(
|
|
133
|
+
ext => ext.name === 'collaborationCaret',
|
|
134
|
+
)
|
|
129
135
|
|
|
130
136
|
const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)
|
|
131
137
|
const collab = collabExtensions.find(ext => ext?.options?.provider)
|
|
@@ -188,8 +194,12 @@ export const UniqueID = Extension.create<UniqueIDOptions>({
|
|
|
188
194
|
// onCreate runs on a deferred setTimeout(0), so a y-sync$ transaction could
|
|
189
195
|
// be applied before it. Setting the flag here (synchronous during editor
|
|
190
196
|
// construction) ensures the first y-sync$ is always handled.
|
|
191
|
-
const collaboration = this.editor.extensionManager.extensions.find(
|
|
192
|
-
|
|
197
|
+
const collaboration = this.editor.extensionManager.extensions.find(
|
|
198
|
+
ext => ext.name === 'collaboration',
|
|
199
|
+
)
|
|
200
|
+
const collaborationCaret = this.editor.extensionManager.extensions.find(
|
|
201
|
+
ext => ext.name === 'collaborationCaret',
|
|
202
|
+
)
|
|
193
203
|
const collabExtensions = [collaboration, collaborationCaret].filter(Boolean)
|
|
194
204
|
const collabWithProvider = collabExtensions.find(ext => ext?.options?.provider)
|
|
195
205
|
|
|
@@ -207,9 +217,11 @@ export const UniqueID = Extension.create<UniqueIDOptions>({
|
|
|
207
217
|
|
|
208
218
|
appendTransaction: (transactions, oldState, newState) => {
|
|
209
219
|
const hasDocChanges =
|
|
210
|
-
transactions.some(transaction => transaction.docChanged) &&
|
|
220
|
+
transactions.some(transaction => transaction.docChanged) &&
|
|
221
|
+
!oldState.doc.eq(newState.doc)
|
|
211
222
|
const filterTransactions =
|
|
212
|
-
this.options.filterTransaction &&
|
|
223
|
+
this.options.filterTransaction &&
|
|
224
|
+
transactions.some(tr => !this.options.filterTransaction?.(tr))
|
|
213
225
|
|
|
214
226
|
const isCollabTransaction = transactions.find(tr => tr.getMeta('y-sync$'))
|
|
215
227
|
|
|
@@ -272,7 +284,9 @@ export const UniqueID = Extension.create<UniqueIDOptions>({
|
|
|
272
284
|
return types.includes(node.type.name)
|
|
273
285
|
})
|
|
274
286
|
|
|
275
|
-
const newIds = newNodes
|
|
287
|
+
const newIds = newNodes
|
|
288
|
+
.map(({ node }) => node.attrs[attributeName])
|
|
289
|
+
.filter(id => id !== null)
|
|
276
290
|
|
|
277
291
|
newNodes.forEach(({ node, pos }, i) => {
|
|
278
292
|
// instead of checking `node.attrs[attributeName]` directly
|
|
@@ -294,7 +308,10 @@ export const UniqueID = Extension.create<UniqueIDOptions>({
|
|
|
294
308
|
|
|
295
309
|
if (nextNode && node.content.size === 0) {
|
|
296
310
|
const nextNodeInTr = tr.doc.nodeAt(nextNode.pos)
|
|
297
|
-
if (
|
|
311
|
+
if (
|
|
312
|
+
nextNodeInTr?.attrs[attributeName] &&
|
|
313
|
+
nextNodeInTr.attrs[attributeName] !== id
|
|
314
|
+
) {
|
|
298
315
|
return
|
|
299
316
|
}
|
|
300
317
|
|