-
Notifications
You must be signed in to change notification settings - Fork 133
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 7 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
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
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,9 @@ | ||
| let counter = 0; | ||
|
|
||
| export function generateBlockUniqueId(type) { | ||
| counter++; | ||
| const prefix = (typeof type === 'string' && type.length ? type : 'b').toLowerCase(); | ||
| const ts = Date.now().toString(36); | ||
| const rand = Math.floor(Math.random() * 0xffffffff).toString(36); | ||
| return `${prefix}-${ts}-${rand}-${counter}`; | ||
| } | ||
167 changes: 167 additions & 0 deletions
167
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,167 @@ | ||
| import { Extension } from '@core/Extension.js'; | ||
| import { helpers } from '@core/index.js'; | ||
| const { findChildren } = helpers; | ||
| import { Plugin } from 'prosemirror-state'; | ||
| import { generateBlockUniqueId } from '@core/utilities/index.js'; | ||
|
|
||
| export const BlockNode = Extension.create({ | ||
| name: 'BlockNode', | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's go with blockNode for consistency (lower case b) in the actual name key |
||
|
|
||
| addCommands() { | ||
| return { | ||
| replaceBlockNodeById: | ||
| (id, contentNode) => | ||
| ({ dispatch, tr }) => { | ||
| let blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id); | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| if (!blockNode || blockNode.length > 1) { | ||
| return true; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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 }) => { | ||
| let blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id); | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| if (!blockNode || blockNode.length > 1) { | ||
| return true; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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 }) => { | ||
| if (!dispatch) return true; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
|
|
||
| let blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id); | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| if (!blockNode || blockNode.length > 1) { | ||
| return true; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| let { pos, node } = blockNode[0]; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| 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) => node.type.groups.includes('block')); | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| }, | ||
|
|
||
| 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 (!node || node?.nodeSize === undefined) { | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| return; | ||
| } | ||
| if (node.type.groups.includes('block')) { | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| blockNodes.push({ | ||
| node, | ||
| pos, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| return blockNodes; | ||
| }, | ||
| }; | ||
| }, | ||
| onCreate() { | ||
| const { state, view } = this.editor; | ||
| const tr = state.tr; | ||
| let changed = false; | ||
|
|
||
| state.doc.descendants((node, pos) => { | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| if (!node.type.spec.group?.split(' ')?.includes('block')) return; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| if (node.attrs?.sdBlockId) return; | ||
| tr.setNodeMarkup( | ||
| pos, | ||
| undefined, | ||
| { | ||
| ...node.attrs, | ||
| sdBlockId: generateBlockUniqueId(node.type.name), | ||
| }, | ||
| node.marks, | ||
| ); | ||
| changed = true; | ||
| return false; | ||
| }); | ||
|
|
||
| if (changed) view.dispatch(tr); | ||
| }, | ||
| addPmPlugins() { | ||
| return [ | ||
| new Plugin({ | ||
|
harbournick marked this conversation as resolved.
|
||
| appendTransaction: (transactions, _oldState, newState) => { | ||
| if (!transactions.some((tr) => tr.docChanged)) return null; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
|
|
||
| let tr = null; | ||
| let changed = false; | ||
| newState.doc.descendants((node, pos) => { | ||
|
harbournick marked this conversation as resolved.
|
||
| if (!node.type.spec.group?.split(' ')?.includes('block')) return; | ||
|
harbournick marked this conversation as resolved.
Outdated
|
||
| if (node.attrs?.sdBlockId) return; | ||
|
|
||
| tr = tr ?? newState.tr; | ||
| tr.setNodeMarkup( | ||
| pos, | ||
| undefined, | ||
| { | ||
| ...node.attrs, | ||
| sdBlockId: generateBlockUniqueId(node.type.name), | ||
| }, | ||
| node.marks, | ||
| ); | ||
| changed = true; | ||
| }); | ||
|
|
||
| return changed ? tr : null; | ||
| }, | ||
| }), | ||
| ]; | ||
| }, | ||
| }); | ||
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 @@ | ||
| export * from './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
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
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
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
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
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
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
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
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
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.