@squiz/formatted-text-editor 1.64.1-6b206ec2.0 → 1.65.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.65.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 402e8b1: Fixed issue where some Remirror text alignment options (eg. start and end) were not correctly transformed to Squiz formatted text structure.
8
+
9
+ ## 1.65.0
10
+
11
+ ### Minor Changes
12
+
13
+ - e11f4e7: Exported function for transforming HTML to formatted text JSON.
14
+
3
15
  ## 1.64.0
4
16
 
5
17
  ### Minor Changes
@@ -29,8 +29,18 @@ const resolveNodeTag = (node) => {
29
29
  exports.resolveNodeTag = resolveNodeTag;
30
30
  const resolveFormattingOptions = (node) => {
31
31
  const formattingOptions = {};
32
- if (node.attrs.nodeTextAlignment) {
33
- formattingOptions.alignment = node.attrs.nodeTextAlignment;
32
+ const textAlignment = node.attrs.nodeTextAlignment;
33
+ const textAlignmentMap = {
34
+ none: undefined,
35
+ left: 'left',
36
+ right: 'right',
37
+ center: 'center',
38
+ start: 'left',
39
+ end: 'right',
40
+ justify: 'justify',
41
+ };
42
+ if (textAlignment && textAlignmentMap[textAlignment]) {
43
+ formattingOptions.alignment = textAlignmentMap[textAlignment];
34
44
  }
35
45
  return formattingOptions;
36
46
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/formatted-text-editor",
3
- "version": "1.64.1-6b206ec2.0",
3
+ "version": "1.65.1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "private": false,
@@ -6,6 +6,7 @@ import { sharedNodeExamples } from '../mocks/squizNodeJson.mock';
6
6
  import { ParagraphExtension, SupExtension } from 'remirror/extensions';
7
7
 
8
8
  type FormattedText = FormattedTextModels.v1.FormattedText;
9
+ type FormattingOptions = FormattedTextModels.v1.FormattingOptions;
9
10
 
10
11
  describe('remirrorNodeToSquizNode', () => {
11
12
  it('should convert a simple Remirror node to a Squiz component JSON object', async () => {
@@ -397,50 +398,60 @@ describe('remirrorNodeToSquizNode', () => {
397
398
  expect(result).toEqual(expected);
398
399
  });
399
400
 
400
- describe('preserve text formatting', () => {
401
- it('should preserve text alignment', async () => {
402
- const content: RemirrorJSON = {
403
- type: 'doc',
404
- content: [
405
- {
406
- type: 'paragraph',
407
- attrs: {
408
- nodeIndent: null,
409
- nodeTextAlignment: 'center',
410
- nodeLineHeight: null,
411
- style: '',
401
+ describe('preserve text formatting - %s', () => {
402
+ it.each([
403
+ ['left', { alignment: 'left' } as FormattingOptions],
404
+ ['right', { alignment: 'right' } as FormattingOptions],
405
+ ['center', { alignment: 'center' } as FormattingOptions],
406
+ ['justify', { alignment: 'justify' } as FormattingOptions],
407
+ ['start', { alignment: 'left' } as FormattingOptions],
408
+ ['end', { alignment: 'right' } as FormattingOptions],
409
+ ['none', undefined],
410
+ [undefined, undefined],
411
+ ])(
412
+ 'should preserve text alignment',
413
+ async (remirrorTextAlignment: string | undefined, expectedFormattingOptions: FormattingOptions | undefined) => {
414
+ const content: RemirrorJSON = {
415
+ type: 'doc',
416
+ content: [
417
+ {
418
+ type: 'paragraph',
419
+ attrs: {
420
+ nodeIndent: null,
421
+ nodeTextAlignment: remirrorTextAlignment,
422
+ nodeLineHeight: null,
423
+ style: '',
424
+ },
425
+ content: [
426
+ {
427
+ type: 'text',
428
+ text: 'Hello world!',
429
+ },
430
+ ],
412
431
  },
413
- content: [
432
+ ],
433
+ };
434
+
435
+ const { editor } = await renderWithEditor(null, { content });
436
+
437
+ const expected: FormattedText = [
438
+ {
439
+ children: [
414
440
  {
415
441
  type: 'text',
416
- text: 'Hello world!',
442
+ value: 'Hello world!',
417
443
  },
418
444
  ],
445
+ formattingOptions: expectedFormattingOptions,
446
+ type: 'tag',
447
+ tag: 'p',
419
448
  },
420
- ],
421
- };
422
-
423
- const { editor } = await renderWithEditor(null, { content });
424
-
425
- const expected: FormattedText = [
426
- {
427
- children: [
428
- {
429
- type: 'text',
430
- value: 'Hello world!',
431
- },
432
- ],
433
- formattingOptions: {
434
- alignment: 'center',
435
- },
436
- type: 'tag',
437
- tag: 'p',
438
- },
439
- ];
449
+ ];
440
450
 
441
- const result = remirrorNodeToSquizNode(editor.doc);
442
- expect(result).toEqual(expected);
443
- });
451
+ const result = remirrorNodeToSquizNode(editor.doc);
452
+ expect(result).toEqual(expected);
453
+ },
454
+ );
444
455
  });
445
456
 
446
457
  it('should handle invalid Remirror node provided', () => {
@@ -14,6 +14,8 @@ type FormattedText = FormattedTextModels.v1.FormattedText;
14
14
  type FormattedNode = FormattedTextModels.v1.FormattedNodes;
15
15
  type FormattedNodeFontProperties = FormattedTextModels.v1.FormattedNodeFontProperties;
16
16
  type FormattedNodeWithChildren = Extract<FormattedNode, { children: FormattedNode[] }>;
17
+ type RemirrorTextAlignment = Exclude<Remirror.Attributes['nodeTextAlignment'], undefined>;
18
+ type FormattedTextAlignment = FormattingOptions['alignment'];
17
19
 
18
20
  export const resolveNodeTag = (node: ProsemirrorNode): string => {
19
21
  if (node.type.name === NodeName.Text) {
@@ -46,9 +48,19 @@ export const resolveNodeTag = (node: ProsemirrorNode): string => {
46
48
 
47
49
  const resolveFormattingOptions = (node: ProsemirrorNode): FormattingOptions => {
48
50
  const formattingOptions: FormattingOptions = {};
51
+ const textAlignment = node.attrs.nodeTextAlignment as RemirrorTextAlignment | undefined;
52
+ const textAlignmentMap: Record<RemirrorTextAlignment, FormattedTextAlignment> = {
53
+ none: undefined,
54
+ left: 'left',
55
+ right: 'right',
56
+ center: 'center',
57
+ start: 'left',
58
+ end: 'right',
59
+ justify: 'justify',
60
+ };
49
61
 
50
- if (node.attrs.nodeTextAlignment) {
51
- formattingOptions.alignment = node.attrs.nodeTextAlignment;
62
+ if (textAlignment && textAlignmentMap[textAlignment]) {
63
+ formattingOptions.alignment = textAlignmentMap[textAlignment];
52
64
  }
53
65
 
54
66
  return formattingOptions;