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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/super-editor/src/core/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './deleteProps.js';
export * from './parseSizeUnit.js';
export * from './minMax.js';
export * from './clipboardUtils.js';
export * from './sdBlockUniqueId.js';
9 changes: 9 additions & 0 deletions packages/super-editor/src/core/utilities/sdBlockUniqueId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let counter = 0;

export function generateBlockUniqueId(type) {
counter++;
Comment thread
harbournick marked this conversation as resolved.
Outdated
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 packages/super-editor/src/extensions/block-node/block-node.js
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',
Copy link
Copy Markdown
Collaborator

@harbournick harbournick Aug 21, 2025

Choose a reason for hiding this comment

The 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);
Comment thread
harbournick marked this conversation as resolved.
Outdated
if (!blockNode || blockNode.length > 1) {
return true;
Comment thread
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);
Comment thread
harbournick marked this conversation as resolved.
Outdated
if (!blockNode || blockNode.length > 1) {
return true;
Comment thread
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;
Comment thread
harbournick marked this conversation as resolved.
Outdated

let blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id);
Comment thread
harbournick marked this conversation as resolved.
Outdated
if (!blockNode || blockNode.length > 1) {
return true;
Comment thread
harbournick marked this conversation as resolved.
Outdated
}

let { pos, node } = blockNode[0];
Comment thread
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'));
Comment thread
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) {
Comment thread
harbournick marked this conversation as resolved.
Outdated
return;
}
if (node.type.groups.includes('block')) {
Comment thread
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) => {
Comment thread
harbournick marked this conversation as resolved.
Outdated
if (!node.type.spec.group?.split(' ')?.includes('block')) return;
Comment thread
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({
Comment thread
harbournick marked this conversation as resolved.
appendTransaction: (transactions, _oldState, newState) => {
if (!transactions.some((tr) => tr.docChanged)) return null;
Comment thread
harbournick marked this conversation as resolved.
Outdated

let tr = null;
let changed = false;
newState.doc.descendants((node, pos) => {
Comment thread
harbournick marked this conversation as resolved.
if (!node.type.spec.group?.split(' ')?.includes('block')) return;
Comment thread
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;
},
}),
];
},
});
1 change: 1 addition & 0 deletions packages/super-editor/src/extensions/block-node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './block-node.js';
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export const BulletList = Node.create({
rendered: false,
},

sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},

attributes: {
rendered: false,
keepOnSplit: true,
Expand Down
8 changes: 8 additions & 0 deletions packages/super-editor/src/extensions/heading/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const Heading = Node.create({
rendered: false,
},
tabStops: { rendered: false },
sdBlockId: {
Comment thread
harbournick marked this conversation as resolved.
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},
};
},

Expand Down
3 changes: 3 additions & 0 deletions packages/super-editor/src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { ShapeContainer } from './shape-container/index.js';
import { ShapeTextbox } from './shape-textbox/index.js';
import { ContentBlock } from './content-block/index.js';
import { StructuredContent, DocumentSection } from './structured-content/index.js';
import { BlockNode } from './block-node/index.js';

// Marks extensions
import { TextStyle } from './text-style/text-style.js';
Expand Down Expand Up @@ -112,6 +113,7 @@ const getRichTextExtensions = () => {
const getStarterExtensions = () => {
return [
Bold,
BlockNode,
BulletList,
Color,
CommentRangeStart,
Expand Down Expand Up @@ -216,6 +218,7 @@ export {
TableHeader,
Placeholder,
DropCursor,
BlockNode,
FieldAnnotation,
fieldAnnotationHelpers,
Image,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ export const OrderedList = Node.create({
},
},

sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},

syncId: {
default: null,
parseDOM: (elem) => elem.getAttribute('data-sync-id'),
Expand Down
8 changes: 8 additions & 0 deletions packages/super-editor/src/extensions/paragraph/paragraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ export const Paragraph = Node.create({
},
},
styleId: {},
sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},
attributes: {
rendered: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export const ShapeContainer = Node.create({
};
},
},

sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},
style: {
renderDOM: (attrs) => {
if (!attrs.style) return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export const ShapeTextbox = Node.create({

addAttributes() {
return {
sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},
attributes: {
rendered: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export const DocumentSection = Node.create({
addAttributes() {
return {
id: {},
sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},
title: {},
description: {},
sectionType: {},
Expand Down
8 changes: 8 additions & 0 deletions packages/super-editor/src/extensions/table/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export const Table = Node.create({
};
},
}, */
sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},

tableIndent: {
renderDOM: ({ tableIndent }) => {
Expand Down
Loading