Skip to content

Commit 95c4d8b

Browse files
committed
Detecting fillable field
1 parent 2ea3d20 commit 95c4d8b

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

packages/super-editor/src/core/super-converter/v2/importer/importerHelpers.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,32 @@ export function hasTextNode(elements) {
9292
const runsHaveText = runs.some((run) => run.elements.some((el) => el.name === 'w:t'));
9393
return runsHaveText;
9494
}
95+
96+
export const isFillableText = (text) => {
97+
const regex = /(\[Insert[^\]]*\]|\[\[[^\]]*\]\]|\{\{[^}]+\}\}|___+|\.{3,}|\[[^\]]+\])/;
98+
return regex.test(text);
99+
};
100+
101+
export const extractFillableParts = (text) => {
102+
const regex = /(\[Insert[^\]]*\]|\[\[[^\]]*\]\]|\{\{[^}]+\}\}|___+|\.{3,}|\[[^\]]+\])/g;
103+
const parts = [];
104+
let lastIndex = 0;
105+
let match;
106+
107+
while ((match = regex.exec(text)) !== null) {
108+
if (match.index > lastIndex) {
109+
parts.push({ text: text.slice(lastIndex, match.index), fillable: false });
110+
}
111+
parts.push({ text: match[0], fillable: true });
112+
lastIndex = regex.lastIndex;
113+
}
114+
115+
if (lastIndex < text.length) {
116+
parts.push({ text: text.slice(lastIndex), fillable: false });
117+
}
118+
119+
return parts;
120+
};
121+
122+
let uniqueIdCounter = 0;
123+
export const generateUniqueId = () => `fillable-${uniqueIdCounter++}`;

packages/super-editor/src/core/super-converter/v2/importer/textNodeImporter.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getElementName, parseProperties } from './importerHelpers.js';
1+
import { generateUniqueId, extractFillableParts, getElementName, parseProperties } from './importerHelpers.js';
22

33
/**
44
* @type {import("docxImporter").NodeHandler}
@@ -24,16 +24,31 @@ export const handleTextNode = (params) => {
2424

2525
// Ignore others - can catch other special cases here if necessary
2626
else return { nodes: [], consumed: 0 };
27+
const parts = extractFillableParts(text);
28+
const resultNodes = parts.flatMap((part) => {
29+
let attrs = { type, attributes: attributes || {} }
30+
if(part.fillable) {
31+
let label = part.text;
32+
attrs.fieldId = generateUniqueId();
33+
attrs.displayLabel = label;
34+
attrs.highlighted = false;
35+
attrs.type = "text";
36+
return {
37+
type: 'fieldAnnotation',
38+
attrs: attrs,
39+
marks,
40+
};
41+
}
42+
return {
43+
type: getElementName(node),
44+
text: part.text,
45+
attrs: attrs,
46+
marks,
47+
};
48+
});
2749

2850
return {
29-
nodes: [
30-
{
31-
type: getElementName(node),
32-
text: text,
33-
attrs: { type, attributes: attributes || {} },
34-
marks,
35-
},
36-
],
51+
nodes: resultNodes,
3752
consumed: 1,
3853
};
3954
};

packages/super-editor/src/core/super-converter/v2/importer/trackChangesImporter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const handleTrackChangeNode = (params) => {
1919
if (['w:ins', 'w:del'].includes(mainNode.name)) {
2020
node = mainNode;
2121
} else {
22-
const sdtContent = mainNode.elements.find((el) => el.name === 'w:sdtContent');
23-
const trackedChange = sdtContent?.elements.find((el) => ['w:ins', 'w:del'].includes(el.name));
22+
const sdtContent = mainNode.elements?.find((el) => el.name === 'w:sdtContent');
23+
const trackedChange = sdtContent?.elements?.find((el) => ['w:ins', 'w:del'].includes(el.name));
2424
if (trackedChange) node = trackedChange;
2525
}
2626

0 commit comments

Comments
 (0)