Skip to content

Commit d1cd146

Browse files
authored
Merge pull request #717 from Harbour-Enterprises/artem-HAR-10180-v2
HAR-10180 - formatting fixes
2 parents 94dc572 + 946a037 commit d1cd146

11 files changed

Lines changed: 112 additions & 39 deletions

File tree

packages/super-editor/src/core/super-converter/SuperConverter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class SuperConverter {
4040
});
4141

4242
static markTypes = [
43-
{ name: 'w:b', type: 'bold' },
44-
{ name: 'w:bCs', type: 'bold' },
43+
{ name: 'w:b', type: 'bold', property: 'value' },
44+
// { name: 'w:bCs', type: 'bold' },
4545
{ name: 'w:i', type: 'italic' },
46-
{ name: 'w:iCs', type: 'italic' },
46+
// { name: 'w:iCs', type: 'italic' },
4747
{ name: 'w:u', type: 'underline', mark: 'underline', property: 'underlineType' },
4848
{ name: 'w:strike', type: 'strike', mark: 'strike' },
4949
{ name: 'w:color', type: 'color', mark: 'textStyle', property: 'color' },
@@ -56,6 +56,7 @@ class SuperConverter {
5656
{ name: 'link', type: 'link', mark: 'link', property: 'href' },
5757
{ name: 'w:highlight', type: 'highlight', mark: 'highlight', property: 'color' },
5858
{ name: 'w:shd', type: 'highlight', mark: 'highlight', property: 'color' },
59+
{ name: 'w:caps', type: 'textTransform', mark: 'textStyle', property: 'textTransform' },
5960
];
6061

6162
static propertyTypes = Object.freeze({

packages/super-editor/src/core/super-converter/exporter.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,9 +1676,17 @@ function translateMark(mark) {
16761676

16771677
const { attrs } = mark;
16781678
let value;
1679-
1679+
16801680
switch (mark.type) {
16811681
case 'bold':
1682+
if (attrs?.value) {
1683+
markElement.attributes['w:val'] = attrs.value;
1684+
} else {
1685+
delete markElement.attributes;
1686+
}
1687+
markElement.type = 'element';
1688+
break;
1689+
16821690
case 'italic':
16831691
delete markElement.attributes;
16841692
markElement.type = 'element';
@@ -1718,6 +1726,15 @@ function translateMark(mark) {
17181726
case 'textIndent':
17191727
markElement.attributes['w:firstline'] = inchesToTwips(attrs.textIndent);
17201728
break;
1729+
1730+
case 'textTransform':
1731+
if (attrs?.textTransform === 'none') {
1732+
markElement.attributes['w:val'] = '0';
1733+
} else {
1734+
delete markElement.attributes;
1735+
}
1736+
markElement.type = 'element';
1737+
break;
17211738

17221739
case 'lineHeight':
17231740
markElement.attributes['w:line'] = linesToTwips(attrs.lineHeight);

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,29 @@ export function parseMarks(property, unknownMarks = [], docx = null) {
4343
const { attributes = {} } = element;
4444
const newMark = { type: m.type };
4545

46-
if (attributes['w:val'] === '0' || attributes['w:val'] === 'none') {
47-
return;
48-
}
49-
50-
// this probably requires a more thorough check.
51-
if (['w:bCs'].includes(m.name) && langAttrs['w:eastAsia']) {
46+
const exceptionMarks = ['w:b', 'w:caps'];
47+
if (
48+
(attributes['w:val'] === '0' || attributes['w:val'] === 'none')
49+
&& !exceptionMarks.includes(m.name)
50+
) {
5251
return;
5352
}
5453

5554
// Use the parent mark (ie: textStyle) if present
5655
if (m.mark) newMark.type = m.mark;
5756

57+
// Special handling of "w:caps".
58+
if (m.name === 'w:caps') {
59+
newMark.attrs = {};
60+
if (attributes['w:val'] === '0') {
61+
newMark.attrs[m.property] = 'none';
62+
} else {
63+
newMark.attrs[m.property] = 'uppercase';
64+
}
65+
marks.push(newMark);
66+
return;
67+
}
68+
5869
// Marks with attrs: we need to get their values
5970
if (Object.keys(attributes).length) {
6071
const value = getMarkValue(m.type, attributes, docx);
@@ -65,6 +76,7 @@ export function parseMarks(property, unknownMarks = [], docx = null) {
6576
newMark.attrs = {};
6677
newMark.attrs[m.property] = value;
6778
}
79+
6880
marks.push(newMark);
6981
});
7082
});

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,6 @@ export const handleParagraphNode = (params) => {
119119
}
120120
}
121121

122-
if (docx) {
123-
const { textCase } = getDefaultParagraphStyle(docx, styleId);
124-
if (textCase) {
125-
schemaNode.attrs.textCase = textCase;
126-
}
127-
}
128-
129122
if (framePr && framePr.attributes['w:dropCap']) {
130123
schemaNode.attrs.dropcap = {
131124
type: framePr.attributes['w:dropCap'],
@@ -270,22 +263,11 @@ const getDefaultParagraphStyle = (docx, styleId = '') => {
270263
let pPrStyleIdSpacingTag = {};
271264
let pPrStyleIdIndentTag = {};
272265
let pPrStyleJc = {};
273-
let textCase = null;
274266
if (styleId) {
275267
const stylesById = styles.elements[0].elements?.find(
276268
(el) => el.name === 'w:style' && el.attributes['w:styleId'] === styleId,
277269
);
278270
const pPrById = stylesById?.elements?.find((el) => el.name === 'w:pPr');
279-
280-
const basedOn = stylesById?.elements.find((el) => el.name === 'w:basedOn');
281-
const baseStyles = styles.elements[0].elements?.find(
282-
(el) => el.name === 'w:style' && el.attributes['w:styleId'] === basedOn?.attributes['w:val']
283-
);
284-
const rprBaseStyles = baseStyles?.elements?.find((el) => el.name === 'w:rPr');
285-
286-
const caps = rprBaseStyles?.elements?.find((el) => el.name === 'w:caps');
287-
if (caps) textCase = 'uppercase';
288-
289271
pPrStyleIdSpacingTag = pPrById?.elements?.find((el) => el.name === 'w:spacing') || {};
290272
pPrStyleIdIndentTag = pPrById?.elements?.find((el) => el.name === 'w:ind') || {};
291273
pPrStyleJc = pPrById?.elements?.find((el) => el.name === 'w:jc') || {};
@@ -312,7 +294,6 @@ const getDefaultParagraphStyle = (docx, styleId = '') => {
312294
spacing: pPrByIdSpacingAttr || spacingRest,
313295
indent: pPrByIdIndentAttr || indentRest,
314296
justify: pPrByIdJcAttr,
315-
textCase,
316297
};
317298
};
318299

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ const handleRunNode = (params) => {
1515
let processedRun = nodeListHandler.handler(childParams)?.filter((n) => n) || [];
1616
const hasRunProperties = node.elements?.some((el) => el.name === 'w:rPr');
1717
const defaultNodeStyles = getMarksFromStyles(docx, parentStyleId);
18+
1819
if (hasRunProperties) {
1920
const { marks = [], attributes = {} } = parseProperties(node);
2021

2122
// Apply fonts from related style definition if there is no marks
2223
const textStyleMark = marks.find((m) => m.type === 'textStyle');
2324
const hasFontStyle = textStyleMark && Object.keys(textStyleMark.attrs).length > 0;
2425
if (defaultNodeStyles.marks && !hasFontStyle) {
25-
marks.push(...defaultNodeStyles.marks);
26+
const hasBoldDisabled = marks.find((m) => m.type === 'bold')?.attrs?.value === '0';
27+
for (let mark of defaultNodeStyles.marks) {
28+
if (['bold'].includes(mark.type) && hasBoldDisabled) continue;
29+
marks.push(mark);
30+
}
2631
}
2732

2833
if (node.marks) marks.push(...node.marks);

packages/super-editor/src/extensions/bold/bold.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ export const Bold = Mark.create({
99
};
1010
},
1111

12+
addAttributes() {
13+
return {
14+
value: {
15+
default: null,
16+
renderDOM: (attrs) => {
17+
if (!attrs.value) return {};
18+
19+
if (attrs.value === '0') {
20+
return { style: 'font-weight: normal' };
21+
}
22+
return {};
23+
},
24+
},
25+
};
26+
},
27+
1228
parseDOM() {
1329
return [
1430
{ tag: 'strong' },

packages/super-editor/src/extensions/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { Strike } from './strike/index.js';
5050
import { Link } from './link/index.js';
5151
import { TrackInsert, TrackDelete, TrackFormat } from './track-changes/index.js';
5252
import { CommentsMark } from './comment/index.js';
53+
import { TextTransform } from './text-transform/index.js';
5354

5455
// Plugins
5556
import { CommentsPlugin } from './comment/index.js';
@@ -174,6 +175,7 @@ const getStarterExtensions = () => {
174175
DocumentSection,
175176
NodeResizer,
176177
CustomSelection,
178+
TextTransform,
177179
];
178180
};
179181

@@ -239,4 +241,5 @@ export {
239241
DocumentSection,
240242
NodeResizer,
241243
CustomSelection,
244+
TextTransform,
242245
};

packages/super-editor/src/extensions/linked-styles/linked-styles.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ export const generateLinkedStyleString = (linkedStyle, basedOnStyle, node, paren
132132
if (!linkedDefinitionStyles['font-size'] && basedOnDefinitionStyles['font-size']) {
133133
resultStyles['font-size'] = basedOnDefinitionStyles['font-size'];
134134
}
135+
if (!linkedDefinitionStyles['text-transform'] && basedOnDefinitionStyles['text-transform']) {
136+
resultStyles['text-transform'] = basedOnDefinitionStyles['text-transform'];
137+
}
135138

136139
Object.entries(resultStyles).forEach(([k, value]) => {
137140
const key = kebabCase(k);
@@ -156,6 +159,8 @@ export const generateLinkedStyleString = (linkedStyle, basedOnStyle, node, paren
156159
const hasParentIndent = Object.keys(parent?.attrs?.indent || {});
157160
const hasParentSpacing = Object.keys(parent?.attrs?.spacing || {});
158161

162+
const listTypes = ['orderedList', 'listItem'];
163+
159164
// If no mark already in the node, we override the style
160165
if (!mark) {
161166
if (key === 'spacing' && includeSpacing && !hasParentSpacing) {
@@ -170,7 +175,17 @@ export const generateLinkedStyleString = (linkedStyle, basedOnStyle, node, paren
170175
if (rightIndent) markValue['margin-right'] = rightIndent + 'px';
171176
if (firstLine) markValue['text-indent'] = firstLine + 'px';
172177
} else if (key === 'bold') {
173-
markValue['font-weight'] = 'bold';
178+
if (!listTypes.includes(node.type.name)) {
179+
markValue['font-weight'] = 'bold';
180+
}
181+
} else if (key === 'text-transform') {
182+
if (!listTypes.includes(node.type.name)) {
183+
markValue[key] = value;
184+
}
185+
} else if (key === 'font-size') {
186+
if (!listTypes.includes(node.type.name)) {
187+
markValue[key] = value;
188+
}
174189
} else if (typeof value === 'string') {
175190
markValue[key] = value;
176191
}

packages/super-editor/src/extensions/paragraph/paragraph.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ export const Paragraph = Node.create({
110110
return { style };
111111
},
112112
},
113-
textCase: {
114-
renderDOM: (attrs) => {
115-
if (!attrs.textCase) return {};
116-
return {
117-
style: `text-transform: ${attrs.textCase}`,
118-
}
119-
},
120-
},
121113
};
122114
},
123115

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './text-transform.js';

0 commit comments

Comments
 (0)