Skip to content

Commit 6ab167f

Browse files
committed
feat: add trackable ids block commands
1 parent 5e6ca71 commit 6ab167f

3 files changed

Lines changed: 65 additions & 13 deletions

File tree

packages/super-editor/src/core/utilities/sdBlockUniqueId.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export function generateBlockUniqueId(type) {
22
const prefix = (typeof type === 'string' && type.length ? type : 'b').toLowerCase();
33
const ts = Date.now().toString(36);
44
const rand = Math.floor(Math.random() * 0xffffffff).toString();
5-
return 'table-12344';
5+
return `${prefix}-${ts}-${rand}`;
66
}

packages/super-editor/src/extensions/block-node/block-node.js

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,72 @@ export const BlockNode = Extension.create({
77

88
addCommands() {
99
return {
10-
replaceBlockNodeById: (id, content) => (params) => {
11-
const { tr } = params;
12-
return null;
13-
},
10+
replaceBlockNodeById:
11+
(id, contentNode) =>
12+
({ dispatch, tr }) => {
13+
let blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id);
14+
if (!blockNode || blockNode.length > 1) {
15+
return true;
16+
}
1417

15-
deleteBlockNodeById: (id) => (params) => {
16-
return null;
17-
},
18+
if (dispatch) {
19+
let { pos, node } = blockNode[0];
20+
let newPosFrom = tr.mapping.map(pos); // map the position between transaction steps
21+
let newPosTo = tr.mapping.map(pos + node.nodeSize);
1822

19-
updateBlockNodeAttrs: (id, attrs) => (params) => {
20-
return null;
21-
},
23+
let currentNode = tr.doc.nodeAt(newPosFrom);
24+
if (node.eq(currentNode)) {
25+
tr.replaceWith(newPosFrom, newPosTo, contentNode);
26+
}
27+
}
28+
29+
return true;
30+
},
31+
32+
deleteBlockNodeById:
33+
(id) =>
34+
({ dispatch, tr }) => {
35+
let blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id);
36+
if (!blockNode || blockNode.length > 1) {
37+
return true;
38+
}
39+
40+
if (dispatch) {
41+
let { pos, node } = blockNode[0];
42+
let newPosFrom = tr.mapping.map(pos); // map the position between transaction steps
43+
let newPosTo = tr.mapping.map(pos + node.nodeSize);
44+
45+
let currentNode = tr.doc.nodeAt(newPosFrom);
46+
if (node.eq(currentNode)) {
47+
tr.delete(newPosFrom, newPosTo);
48+
}
49+
}
50+
51+
return true;
52+
},
53+
54+
updateBlockNodeAttributes:
55+
(id, attrs = {}) =>
56+
({ dispatch, tr }) => {
57+
if (!dispatch) return true;
58+
59+
let blockNode = this.editor.helpers.BlockNode.getBlockNodeById(id);
60+
if (!blockNode || blockNode.length > 1) {
61+
return true;
62+
}
63+
64+
let { pos, node } = blockNode[0];
65+
let newPos = tr.mapping.map(pos);
66+
let currentNode = tr.doc.nodeAt(newPos);
67+
if (node.eq(currentNode)) {
68+
tr.setNodeMarkup(newPos, undefined, {
69+
...node.attrs,
70+
...attrs,
71+
});
72+
}
73+
74+
return true;
75+
},
2276
};
2377
},
2478

packages/superdoc/src/dev/components/SuperdocDev.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ const onContentError = ({ editor, error, documentId, file }) => {
147147
};
148148
149149
const exportDocx = async (commentsType) => {
150-
console.log(editor.helpers.BlockNode.getBlockNodesInRange(2000, 4000));
151-
152150
console.debug('Exporting docx', { commentsType });
153151
await superdoc.value.export({ commentsType });
154152
};

0 commit comments

Comments
 (0)