Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
71 changes: 68 additions & 3 deletions packages/super-editor/src/core/super-converter/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1731,17 +1731,25 @@ function prepareCheckboxAnnotation(params) {
*/
function prepareHtmlAnnotation(params) {
const {
node: { attrs = {} },
node: { attrs = {}, marks = [] },
editorSchema,
} = params;

const parser = new window.DOMParser();
const paragraphHtml = parser.parseFromString(attrs.rawHtml || attrs.displayLabel, 'text/html');
const marksFromAttrs = translateFieldAttrsToMarks(attrs);
const allMarks = [...marks, ...marksFromAttrs]

const state = EditorState.create({
doc: PMDOMParser.fromSchema(params.editorSchema).parse(paragraphHtml),
let state = EditorState.create({
doc: PMDOMParser.fromSchema(editorSchema).parse(paragraphHtml),
});

if (allMarks.length) {
state = applyMarksToHtmlAnnotation(state, allMarks);
}

const htmlAnnotationNode = state.doc.toJSON();

return {
name: 'htmlAnnotation',
elements: translateChildNodes({
Expand Down Expand Up @@ -1896,6 +1904,20 @@ function translateFieldAnnotation(params) {
'w:val': attrs.multipleImage,
},
},
{
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save fontFamily and fontSize as attributes to re-import annotations correctly.

name: 'w:fieldFontFamily',
attributes: {
'xmlns:w': customXmlns,
'w:val': attrs.fontFamily,
},
},
{
name: 'w:fieldFontSize',
attributes: {
'xmlns:w': customXmlns,
'w:val': attrs.fontSize,
},
},
],
},
{
Expand Down Expand Up @@ -2103,3 +2125,46 @@ function resizeKeepAspectRatio(width, height, maxWidth) {
}
return { width, height };
}

function applyMarksToHtmlAnnotation(state, marks) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply font marks (*attributes as marks) of html annotation to inner content of annotation, but do not override if text node already has font styles. This is necessary for the final doc export, so the paragraph text look the same (as annotation).

const { tr, doc, schema } = state;
const allowedMarks = ['fontFamily', 'fontSize'];

if (
!marks.some((m) => allowedMarks.includes(m.type))
) {
return state;
}

const fontFamily = marks.find((m) => m.type === 'fontFamily');
const fontSize = marks.find((m) => m.type === 'fontSize');

doc.descendants((node, pos) => {
if (!node.isText) return;

const found = node.marks.find((m) => m.type.name === 'textStyle');
const textStyleType = schema.marks.textStyle;

if (!found) {
tr.addMark(pos, pos + node.nodeSize, textStyleType.create({
...fontFamily?.attrs,
...fontSize?.attrs,
}));
return;
}

if (!found?.attrs.fontFamily && fontFamily) {
tr.addMark(pos, pos + node.nodeSize, textStyleType.create({
...found?.attrs,
...fontFamily.attrs,
}));
} else if (!found?.attrs.fontSize && fontSize) {
tr.addMark(pos, pos + node.nodeSize, textStyleType.create({
...found?.attrs,
...fontSize.attrs,
}));
}
});

return state.apply(tr);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const handleAnnotationNode = (params) => {
const node = nodes[0];
const sdtPr = node.elements.find((el) => el.name === 'w:sdtPr');
const sdtContent = node.elements.find((el) => el.name === 'w:sdtContent');
const { attrs: marksAsAttrs, marks } = parseAnnotationMarks(sdtContent);
const type = sdtPr?.elements.find((el) => el.name === 'w:fieldTypeShort')?.attributes['w:val'];
const { attrs: marksAsAttrs, marks } = parseAnnotationMarks(sdtContent, type);

const docPartObj = sdtPr?.elements.find((el) => el.name === 'w:docPartObj');
if (docPartObj) {
Expand All @@ -23,9 +24,10 @@ export const handleAnnotationNode = (params) => {
const alias = sdtPr?.elements.find((el) => el.name === 'w:alias');
const tag = sdtPr?.elements.find((el) => el.name === 'w:tag');
const fieldType = sdtPr?.elements.find((el) => el.name === 'w:fieldType')?.attributes['w:val'];
const type = sdtPr?.elements.find((el) => el.name === 'w:fieldTypeShort')?.attributes['w:val'];
const fieldColor = sdtPr?.elements.find((el) => el.name === 'w:fieldColor')?.attributes['w:val'];
const isMultipleImage = sdtPr?.elements.find((el) => el.name === 'w:fieldMultipleImage')?.attributes['w:val'];
const fontFamily = sdtPr?.elements.find((el) => el.name === 'w:fieldFontFamily')?.attributes['w:val'];
const fontSize = sdtPr?.elements.find((el) => el.name === 'w:fieldFontSize')?.attributes['w:val'];

const attrs = {
type,
Expand All @@ -34,23 +36,27 @@ export const handleAnnotationNode = (params) => {
fieldType,
fieldColor,
multipleImage: isMultipleImage === 'true',
fontFamily: fontFamily !== 'null' ? fontFamily : null,
fontSize: fontSize !== 'null' ? fontSize : null,
};

const allAttrs = { ...attrs, ...marksAsAttrs };
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For regular text fields, fontFamily and fontSize will be in attrs and marksAsAttrs. It will be overridden from the last object.

For html fields, fontFamily and fontSize will be only in attrs object.


if (!attrs.fieldId || !attrs.displayLabel) {
return { nodes: [], consumed: 0 };
}

let result = {
type: 'text',
text: `{{${attrs.displayLabel}}}`,
attrs: { ...attrs, ...marksAsAttrs },
attrs: allAttrs,
marks,
};

if (params.editor.options.annotations) {
result = {
type: 'fieldAnnotation',
attrs: { ...attrs, ...marksAsAttrs }
attrs: allAttrs,
};
};

Expand All @@ -65,8 +71,20 @@ export const handleAnnotationNode = (params) => {
* @param {Object} content The sdtContent node
* @returns {Object} The attributes object
*/
export const parseAnnotationMarks = (content = {}) => {
const run = content.elements?.find((el) => el.name === 'w:r');
export const parseAnnotationMarks = (content = {}, type) => {
let mainContent = content;

if (type === 'html') {
/// Note: html annotation has a different structure and can include
/// several paragraphs with different styles. We could find the first paragraph
/// and take the marks from there, but we take fontFamily and fontSize from the annotation attributes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no processing for html field, since getting fontFamily and fontSize from attributes is a more correct solution in my opinion.


/// Example:
/// const firstPar = content.elements?.find((el) => el.name === 'w:p');
/// if (firstPar) mainContent = firstPar;
}

const run = mainContent.elements?.find((el) => el.name === 'w:r');
const rPr = run?.elements?.find((el) => el.name === 'w:rPr');
if (!rPr) return {};

Expand Down
10 changes: 10 additions & 0 deletions packages/super-editor/src/dev/components/DeveloperPlayground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ const onCreate = ({ editor }) => {

// Set debugging pagination value from editor plugin state
isDebuggingPagination.value = PaginationPluginKey.getState(editor.state)?.isDebugging;

// editor.commands.addFieldAnnotation(0, {
// type: 'html',
// displayLabel: 'Paragraph',
// fieldId: `123`,
// fieldType: 'TEXTINPUT',
// fieldColor: '#980043',
// rawHtml: '<p><span style="font-family: Courier New; font-size: 8pt;">Par 1</span></p><p>Par 2</p>',
// });
};

const onCommentClicked = ({ conversation }) => {
Expand All @@ -68,6 +77,7 @@ const editorOptions = computed(() => {
users: [], // For comment @-mentions, only users that have access to the document
pagination: true,
telemetry: telemetry.value,
annotations: true,
}
});

Expand Down
Loading