-
Notifications
You must be signed in to change notification settings - Fork 151
HAR-10208 - Trackable id #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
256759a
feat: adding node block unique ids
falsaadehh 5e6ca71
Merge branch 'develop' of github.com:Harbour-Enterprises/SuperDoc int…
falsaadehh 6ab167f
feat: add trackable ids block commands
falsaadehh c9695f3
fix: same id for nodes of same type
falsaadehh 8e44f6f
fix: same id for nodes of same type
falsaadehh 7d82c55
fix: same id for nodes of same type
falsaadehh a4b06bf
fix: same id for nodes of same type
falsaadehh 467ff8c
Feature: Block node IDs utilities, add hasInitialized to run plugin w…
harbournick 9019fa1
fix: consider code review comments
falsaadehh ce22284
test: adding block-node commands and helpers tests
falsaadehh 5c7780b
fix: change plugin name
falsaadehh 9b5c7bc
Merge branch 'develop' into trackable_ids
chittolinag 58c56ea
Merge branch 'develop' into trackable_ids
harbournick ef0ac43
chore: fix helpers blockNode capitalization namespace issue
harbournick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
packages/super-editor/src/extensions/block-node/block-node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { Extension } from '@core/Extension.js'; | ||
| import { helpers } from '@core/index.js'; | ||
| import { Plugin, PluginKey } from 'prosemirror-state'; | ||
| import { ReplaceStep } from 'prosemirror-transform'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| const { findChildren } = helpers; | ||
| const SD_BLOCK_ID_ATTRIBUTE_NAME = 'sdBlockId'; | ||
| export const BlockNodePluginKey = new PluginKey('blockNodePlugin'); | ||
| export const BlockNode = Extension.create({ | ||
| name: 'blockNode', | ||
|
|
||
| addCommands() { | ||
| return { | ||
| replaceBlockNodeById: | ||
| (id, contentNode) => | ||
| ({ dispatch, tr }) => { | ||
| const blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id); | ||
| if (!blockNode || blockNode.length > 1) { | ||
| return false; | ||
| } | ||
|
|
||
| if (dispatch) { | ||
| let { pos, node } = blockNode[0]; | ||
| let newPosFrom = tr.mapping.map(pos); | ||
| let newPosTo = tr.mapping.map(pos + node.nodeSize); | ||
|
|
||
| let currentNode = tr.doc.nodeAt(newPosFrom); | ||
| if (node.eq(currentNode)) { | ||
| tr.replaceWith(newPosFrom, newPosTo, contentNode); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }, | ||
|
|
||
| deleteBlockNodeById: | ||
| (id) => | ||
| ({ dispatch, tr }) => { | ||
| const blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id); | ||
| if (!blockNode || blockNode.length > 1) { | ||
| return false; | ||
| } | ||
|
|
||
| if (dispatch) { | ||
| let { pos, node } = blockNode[0]; | ||
| let newPosFrom = tr.mapping.map(pos); | ||
| let newPosTo = tr.mapping.map(pos + node.nodeSize); | ||
|
|
||
| let currentNode = tr.doc.nodeAt(newPosFrom); | ||
| if (node.eq(currentNode)) { | ||
| tr.delete(newPosFrom, newPosTo); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }, | ||
|
|
||
| updateBlockNodeAttributes: | ||
| (id, attrs = {}) => | ||
| ({ dispatch, tr }) => { | ||
| const blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id); | ||
| if (!blockNode || blockNode.length > 1) { | ||
| return false; | ||
| } | ||
| if (dispatch) { | ||
| let { pos, node } = blockNode[0]; | ||
| let newPos = tr.mapping.map(pos); | ||
| let currentNode = tr.doc.nodeAt(newPos); | ||
| if (node.eq(currentNode)) { | ||
| tr.setNodeMarkup(newPos, undefined, { | ||
| ...node.attrs, | ||
| ...attrs, | ||
| }); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| addHelpers() { | ||
| return { | ||
| getBlockNodes: () => { | ||
| return findChildren(this.editor.state.doc, (node) => nodeAllowsSdBlockIdAttr(node)); | ||
| }, | ||
|
|
||
| getBlockNodeById: (id) => { | ||
| return findChildren(this.editor.state.doc, (node) => node.attrs.sdBlockId === id); | ||
| }, | ||
|
|
||
| getBlockNodesByType: (type) => { | ||
| return findChildren(this.editor.state.doc, (node) => node.type.name === type); | ||
| }, | ||
|
|
||
| getBlockNodesInRange: (from, to) => { | ||
| let blockNodes = []; | ||
|
|
||
| this.editor.state.doc.nodesBetween(from, to, (node, pos) => { | ||
| if (nodeAllowsSdBlockIdAttr(node)) { | ||
| blockNodes.push({ | ||
| node, | ||
| pos, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| return blockNodes; | ||
| }, | ||
| }; | ||
| }, | ||
| addPmPlugins() { | ||
| let hasInitialized = false; | ||
|
|
||
| return [ | ||
| new Plugin({ | ||
| key: BlockNodePluginKey, | ||
| appendTransaction: (transactions, _oldState, newState) => { | ||
| if (hasInitialized && !transactions.some((tr) => tr.docChanged)) return null; | ||
|
|
||
| // Check for new block nodes and if none found, we don't need to do anything | ||
| if (hasInitialized && !checkForNewBlockNodesInTrs(transactions)) return null; | ||
|
|
||
| let tr = null; | ||
| let changed = false; | ||
| newState.doc.descendants((node, pos) => { | ||
|
harbournick marked this conversation as resolved.
|
||
| // Only allow block nodes with a valid sdBlockId attribute | ||
| if (!nodeAllowsSdBlockIdAttr(node) || !nodeNeedsSdBlockId(node)) return null; | ||
|
|
||
| tr = tr ?? newState.tr; | ||
| tr.setNodeMarkup( | ||
| pos, | ||
| undefined, | ||
| { | ||
| ...node.attrs, | ||
| sdBlockId: uuidv4(), | ||
| }, | ||
| node.marks, | ||
| ); | ||
| changed = true; | ||
| }); | ||
|
|
||
| if (changed && !hasInitialized) hasInitialized = true; | ||
| return changed ? tr : null; | ||
| }, | ||
| }), | ||
| ]; | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * Check if a node allows sdBlockId attribute | ||
| * @param {import("prosemirror-model").Node} node - The node to check | ||
| * @returns {boolean} - True if the node type supports sdBlockId attribute | ||
| */ | ||
| export const nodeAllowsSdBlockIdAttr = (node) => { | ||
| return !!(node?.isBlock && node?.type?.spec?.attrs?.[SD_BLOCK_ID_ATTRIBUTE_NAME]); | ||
| }; | ||
|
|
||
| /** | ||
| * Check if a node needs an sdBlockId (doesn't have one or has null/empty value) | ||
| * @param {import("prosemirror-model").Node} node - The node to check | ||
| * @returns {boolean} - True if the node needs an sdBlockId assigned | ||
| */ | ||
| export const nodeNeedsSdBlockId = (node) => { | ||
| const currentId = node?.attrs?.[SD_BLOCK_ID_ATTRIBUTE_NAME]; | ||
| return !currentId; | ||
| }; | ||
|
|
||
| /** | ||
| * Check for new block nodes in ProseMirror transactions. | ||
| * Iterate through the list of transactions, and in each tr check if there are any new block nodes. | ||
| * @param {Array<Transaction>} transactions - The ProseMirror transactions to check. | ||
| * @returns {boolean} - True if new block nodes are found, false otherwise. | ||
| */ | ||
| export const checkForNewBlockNodesInTrs = (transactions) => { | ||
| return transactions.some((tr) => { | ||
| return tr.steps.some((step) => { | ||
| const hasValidSdBlockNodes = step.slice?.content?.content?.some((node) => nodeAllowsSdBlockIdAttr(node)); | ||
| return step instanceof ReplaceStep && hasValidSdBlockNodes; | ||
| }); | ||
| }); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.