@folijs/core 0.4.0 → 0.6.0

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.
Files changed (153) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/model/schema.d.ts +9 -1
  3. package/dist/model/schema.d.ts.map +1 -1
  4. package/dist/model/schema.js +13 -3
  5. package/dist/model/schema.js.map +1 -1
  6. package/dist/state/commands.d.ts +10 -1
  7. package/dist/state/commands.d.ts.map +1 -1
  8. package/dist/state/commands.js +125 -40
  9. package/dist/state/commands.js.map +1 -1
  10. package/dist/state/comments.d.ts.map +1 -1
  11. package/dist/state/comments.js +15 -6
  12. package/dist/state/comments.js.map +1 -1
  13. package/dist/state/controls.d.ts +128 -0
  14. package/dist/state/controls.d.ts.map +1 -0
  15. package/dist/state/controls.js +1459 -0
  16. package/dist/state/controls.js.map +1 -0
  17. package/dist/state/editor.d.ts +86 -1
  18. package/dist/state/editor.d.ts.map +1 -1
  19. package/dist/state/editor.js +169 -41
  20. package/dist/state/editor.js.map +1 -1
  21. package/dist/state/fields.d.ts +79 -0
  22. package/dist/state/fields.d.ts.map +1 -0
  23. package/dist/state/fields.js +398 -0
  24. package/dist/state/fields.js.map +1 -0
  25. package/dist/state/fieldupdate.d.ts +178 -0
  26. package/dist/state/fieldupdate.d.ts.map +1 -0
  27. package/dist/state/fieldupdate.js +1574 -0
  28. package/dist/state/fieldupdate.js.map +1 -0
  29. package/dist/state/find.d.ts +68 -0
  30. package/dist/state/find.d.ts.map +1 -0
  31. package/dist/state/find.js +397 -0
  32. package/dist/state/find.js.map +1 -0
  33. package/dist/state/history.d.ts +12 -1
  34. package/dist/state/history.d.ts.map +1 -1
  35. package/dist/state/history.js +43 -7
  36. package/dist/state/history.js.map +1 -1
  37. package/dist/state/index.d.ts +15 -5
  38. package/dist/state/index.d.ts.map +1 -1
  39. package/dist/state/index.js +13 -3
  40. package/dist/state/index.js.map +1 -1
  41. package/dist/state/links.d.ts.map +1 -1
  42. package/dist/state/links.js +30 -19
  43. package/dist/state/links.js.map +1 -1
  44. package/dist/state/mark.d.ts.map +1 -1
  45. package/dist/state/mark.js +8 -1
  46. package/dist/state/mark.js.map +1 -1
  47. package/dist/state/navigation.d.ts +70 -0
  48. package/dist/state/navigation.d.ts.map +1 -0
  49. package/dist/state/navigation.js +226 -0
  50. package/dist/state/navigation.js.map +1 -0
  51. package/dist/state/notes.d.ts +94 -0
  52. package/dist/state/notes.d.ts.map +1 -0
  53. package/dist/state/notes.js +444 -0
  54. package/dist/state/notes.js.map +1 -0
  55. package/dist/state/pictures.d.ts +51 -0
  56. package/dist/state/pictures.d.ts.map +1 -0
  57. package/dist/state/pictures.js +140 -0
  58. package/dist/state/pictures.js.map +1 -0
  59. package/dist/state/review.d.ts +13 -4
  60. package/dist/state/review.d.ts.map +1 -1
  61. package/dist/state/review.js +223 -29
  62. package/dist/state/review.js.map +1 -1
  63. package/dist/state/sections.d.ts +101 -0
  64. package/dist/state/sections.d.ts.map +1 -0
  65. package/dist/state/sections.js +472 -0
  66. package/dist/state/sections.js.map +1 -0
  67. package/dist/state/selection.d.ts +12 -1
  68. package/dist/state/selection.d.ts.map +1 -1
  69. package/dist/state/selection.js +12 -0
  70. package/dist/state/selection.js.map +1 -1
  71. package/dist/state/state.d.ts +2 -0
  72. package/dist/state/state.d.ts.map +1 -1
  73. package/dist/state/state.js +4 -0
  74. package/dist/state/state.js.map +1 -1
  75. package/dist/state/stories.d.ts +41 -0
  76. package/dist/state/stories.d.ts.map +1 -0
  77. package/dist/state/stories.js +243 -0
  78. package/dist/state/stories.js.map +1 -0
  79. package/dist/state/tablecommands.d.ts +77 -0
  80. package/dist/state/tablecommands.d.ts.map +1 -0
  81. package/dist/state/tablecommands.js +675 -0
  82. package/dist/state/tablecommands.js.map +1 -0
  83. package/dist/state/tables.d.ts +110 -0
  84. package/dist/state/tables.d.ts.map +1 -0
  85. package/dist/state/tables.js +730 -0
  86. package/dist/state/tables.js.map +1 -0
  87. package/dist/state/track.d.ts +23 -0
  88. package/dist/state/track.d.ts.map +1 -1
  89. package/dist/state/track.js +113 -15
  90. package/dist/state/track.js.map +1 -1
  91. package/dist/state/word.d.ts +78 -16
  92. package/dist/state/word.d.ts.map +1 -1
  93. package/dist/state/word.js +231 -11
  94. package/dist/state/word.js.map +1 -1
  95. package/dist/state/wordclip.d.ts +15 -6
  96. package/dist/state/wordclip.d.ts.map +1 -1
  97. package/dist/state/wordclip.js +213 -18
  98. package/dist/state/wordclip.js.map +1 -1
  99. package/dist/state/wordformat.d.ts +10 -2
  100. package/dist/state/wordformat.d.ts.map +1 -1
  101. package/dist/state/wordformat.js +98 -17
  102. package/dist/state/wordformat.js.map +1 -1
  103. package/dist/v3/document.d.ts +237 -7
  104. package/dist/v3/document.d.ts.map +1 -1
  105. package/dist/v3/document.js +314 -15
  106. package/dist/v3/document.js.map +1 -1
  107. package/dist/v3/index.d.ts +2 -2
  108. package/dist/v3/index.d.ts.map +1 -1
  109. package/dist/v3/index.js +2 -2
  110. package/dist/v3/index.js.map +1 -1
  111. package/dist/v3/migrate.d.ts +2 -2
  112. package/dist/v3/migrate.d.ts.map +1 -1
  113. package/dist/v3/resolve.d.ts +57 -5
  114. package/dist/v3/resolve.d.ts.map +1 -1
  115. package/dist/v3/resolve.js +224 -16
  116. package/dist/v3/resolve.js.map +1 -1
  117. package/dist/v3/schema.d.ts +6 -0
  118. package/dist/v3/schema.d.ts.map +1 -1
  119. package/dist/v3/schema.js +254 -8
  120. package/dist/v3/schema.js.map +1 -1
  121. package/package.json +1 -1
  122. package/src/model/schema.ts +18 -2
  123. package/src/state/commands.ts +121 -32
  124. package/src/state/comments.ts +14 -6
  125. package/src/state/controls.ts +1596 -0
  126. package/src/state/editor.ts +292 -46
  127. package/src/state/fields.ts +432 -0
  128. package/src/state/fieldupdate.ts +1770 -0
  129. package/src/state/find.ts +510 -0
  130. package/src/state/history.ts +51 -7
  131. package/src/state/index.ts +157 -0
  132. package/src/state/links.ts +27 -17
  133. package/src/state/mark.ts +8 -1
  134. package/src/state/navigation.ts +305 -0
  135. package/src/state/notes.ts +519 -0
  136. package/src/state/numeric-pictures.word.json +172 -0
  137. package/src/state/pictures.ts +168 -0
  138. package/src/state/review.ts +265 -31
  139. package/src/state/sections.ts +571 -0
  140. package/src/state/selection.ts +15 -1
  141. package/src/state/state.ts +5 -0
  142. package/src/state/stories.ts +285 -0
  143. package/src/state/tablecommands.ts +741 -0
  144. package/src/state/tables.ts +761 -0
  145. package/src/state/track.ts +128 -16
  146. package/src/state/word.ts +276 -13
  147. package/src/state/wordclip.ts +231 -32
  148. package/src/state/wordformat.ts +96 -14
  149. package/src/v3/document.ts +572 -16
  150. package/src/v3/index.ts +21 -0
  151. package/src/v3/migrate.ts +2 -2
  152. package/src/v3/resolve.ts +289 -21
  153. package/src/v3/schema.ts +278 -9
@@ -0,0 +1,172 @@
1
+ {
2
+ "word": "16.0, Spanish separators swapped back",
3
+ "values": [0, 5, 12, 1234.5, 1234567.891, -5, -1234.5, 0.5, 0.126],
4
+ "pictures": [
5
+ "0",
6
+ "00",
7
+ "000",
8
+ "#",
9
+ "##",
10
+ "###",
11
+ "####",
12
+ "#,##0",
13
+ "#,##0.00",
14
+ "0.00",
15
+ "0.0",
16
+ "#.##",
17
+ "#.00",
18
+ "##.##",
19
+ "x",
20
+ "xx",
21
+ "x#",
22
+ "x0",
23
+ "0x",
24
+ "##x",
25
+ "0.0x",
26
+ "0.x",
27
+ "0.xx",
28
+ "x.x",
29
+ "$#,##0.00",
30
+ "$0",
31
+ "0 $",
32
+ "0%",
33
+ "#%",
34
+ "+0",
35
+ "-0",
36
+ "+#",
37
+ "-#",
38
+ "+0.0",
39
+ "-0.0",
40
+ "0+",
41
+ "0-",
42
+ "0;-0",
43
+ "0;(0)",
44
+ "0;(0);'zero'",
45
+ "0;;",
46
+ ";;",
47
+ "#;#;#",
48
+ "'a#b' 0",
49
+ "0 'units'",
50
+ "'x'0",
51
+ "'0'0",
52
+ "#,#",
53
+ "0,0",
54
+ "0,000",
55
+ "0.00;-0.00;0",
56
+ "00.00",
57
+ ",0",
58
+ "0.",
59
+ ".0",
60
+ "#.#",
61
+ "abc",
62
+ "a0b",
63
+ "0 a",
64
+ "*0",
65
+ "(0)",
66
+ "#,##0;(#,##0)"
67
+ ],
68
+ "results": [
69
+ ["0", "5", "12", "1235", "1234568", "-5", "-1235", "1", "0"],
70
+ ["00", "05", "12", "1235", "1234568", "-05", "-1235", "01", "00"],
71
+ ["000", "005", "012", "1235", "1234568", "-005", "-1235", "001", "000"],
72
+ [" ", "5", "12", "1235", "1234568", "-5", "-1235", "1", " "],
73
+ [" ", " 5", "12", "1235", "1234568", "- 5", "-1235", " 1", " "],
74
+ [" ", " 5", " 12", "1235", "1234568", "- 5", "-1235", " 1", " "],
75
+ [" ", " 5", " 12", "1235", "1234568", "- 5", "-1235", " 1", " "],
76
+ [" 0", " 5", " 12", "1,235", "1,234,568", "- 5", "-1,235", " 1", " 0"],
77
+ [
78
+ " 0.00",
79
+ " 5.00",
80
+ " 12.00",
81
+ "1,234.50",
82
+ "1,234,567.89",
83
+ "- 5.00",
84
+ "-1,234.50",
85
+ " 0.50",
86
+ " 0.13"
87
+ ],
88
+ ["0.00", "5.00", "12.00", "1234.50", "1234567.89", "-5.00", "-1234.50", "0.50", "0.13"],
89
+ ["0.0", "5.0", "12.0", "1234.5", "1234567.9", "-5.0", "-1234.5", "0.5", "0.1"],
90
+ [" .0 ", "5. ", "12. ", "1234.5 ", "1234567.89", "-5. ", "-1234.5 ", " .5 ", " .13"],
91
+ [" .00", "5.00", "12.00", "1234.50", "1234567.89", "-5.00", "-1234.50", " .50", " .13"],
92
+ [" .0 ", " 5. ", "12. ", "1234.5 ", "1234567.89", "- 5. ", "-1234.5 ", " .5 ", " .13"],
93
+ [" ", "5", "2", "5", "8", "5", "5", "1", " "],
94
+ [" ", " 5", "12", "35", "68", " 5", "35", " 1", " "],
95
+ [" ", " 5", "12", "35", "68", " -5", "35", " 1", " "],
96
+ [" 0", " 5", "12", "35", "68", " -5", "35", " 1", " 0"],
97
+ ["0 ", "05", "12", "1235", "1234568", "-05", "-1235", "01", "0 "],
98
+ [" ", " 5", " 12", "1235", "1234568", "- 5", "-1235", " 1", " "],
99
+ ["0.0", "5.0", "12.0", "1234.5", "1234567.89", "-5.0", "-1234.5", "0.5", "0.13"],
100
+ ["0.0", "5.", "12.", "1234.5", "1234567.9", "-5.", "-1234.5", "0.5", "0.1"],
101
+ ["0.0", "5.", "12.", "1234.5", "1234567.89", "-5.", "-1234.5", "0.5", "0.13"],
102
+ [" .0", "5.", "2.", "4.5", "7.9", "5.", "4.5", " .5", " .1"],
103
+ [
104
+ "$ 0.00",
105
+ "$ 5.00",
106
+ "$ 12.00",
107
+ "$1,234.50",
108
+ "$1,234,567.89",
109
+ "$- 5.00",
110
+ "$-1,234.50",
111
+ "$ 0.50",
112
+ "$ 0.13"
113
+ ],
114
+ ["$0", "$5", "$12", "$1235", "$1234568", "$-5", "$-1235", "$1", "$0"],
115
+ ["0 $", "5 $", "12 $", "1235 $", "1234568 $", "-5 $", "-1235 $", "1 $", "0 $"],
116
+ ["0%", "5%", "12%", "1235%", "1234568%", "-5%", "-1235%", "1%", "0%"],
117
+ [" %", "5%", "12%", "1235%", "1234568%", "-5%", "-1235%", "1%", " %"],
118
+ [" 0", "+5", "+12", "+1235", "+1234568", "-5", "-1235", "+1", " 0"],
119
+ [" 0", " 5", " 12", " 1235", " 1234568", "-5", "-1235", " 1", " 0"],
120
+ [" ", "+5", "+12", "+1235", "+1234568", "-5", "-1235", "+1", " "],
121
+ [" ", " 5", " 12", " 1235", " 1234568", "-5", "-1235", " 1", " "],
122
+ [" 0.0", "+5.0", "+12.0", "+1234.5", "+1234567.9", "-5.0", "-1234.5", "+0.5", "+0.1"],
123
+ [" 0.0", " 5.0", " 12.0", " 1234.5", " 1234567.9", "-5.0", "-1234.5", " 0.5", " 0.1"],
124
+ ["0 ", "5+", "12+", "1235+", "1234568+", "5-", "1235-", "1+", "0 "],
125
+ ["0 ", "5 ", "12 ", "1235 ", "1234568 ", "5-", "1235-", "1 ", "0 "],
126
+ ["0", "5", "12", "1235", "1234568", "-5", "-1235", "1", "0"],
127
+ ["0", "5", "12", "1235", "1234568", "(5)", "(1235)", "1", "0"],
128
+ ["zero", "5", "12", "1235", "1234568", "(5)", "(1235)", "1", "0"],
129
+ ["", "5", "12", "1235", "1234568", "", "", "1", "0"],
130
+ ["", "", "", "", "", "", "", "", ""],
131
+ [" ", "5", "12", "1235", "1234568", "5", "1235", "1", " "],
132
+ [
133
+ "a#b 0",
134
+ "a#b 5",
135
+ "a#b 12",
136
+ "a#b 1235",
137
+ "a#b 1234568",
138
+ "a#b -5",
139
+ "a#b -1235",
140
+ "a#b 1",
141
+ "a#b 0"
142
+ ],
143
+ [
144
+ "0 units",
145
+ "5 units",
146
+ "12 units",
147
+ "1235 units",
148
+ "1234568 units",
149
+ "-5 units",
150
+ "-1235 units",
151
+ "1 units",
152
+ "0 units"
153
+ ],
154
+ ["x0", "x5", "x12", "x1235", "x1234568", "x-5", "x-1235", "x1", "x0"],
155
+ ["00", "05", "012", "01235", "01234568", "0-5", "0-1235", "01", "00"],
156
+ [" ", " 5", "12", "1,235", "1,234,568", "- 5", "-1,235", " 1", " "],
157
+ ["00", "05", "12", "1,235", "1,234,568", "-05", "-1,235", "01", "00"],
158
+ ["0,000", "0,005", "0,012", "1,235", "1,234,568", "-0,005", "-1,235", "0,001", "0,000"],
159
+ ["0", "5.00", "12.00", "1234.50", "1234567.89", "-5.00", "-1234.50", "0.50", "0.13"],
160
+ ["00.00", "05.00", "12.00", "1234.50", "1234567.89", "-05.00", "-1234.50", "00.50", "00.13"],
161
+ ["0", "5", "12", "1,235", "1,234,568", "-5", "-1,235", "1", "0"],
162
+ ["0.", "5.", "12.", "1235.", "1234568.", "-5.", "-1235.", "1.", "0."],
163
+ [".0", ".0", ".0", ".5", ".9", ".0", ".5", ".5", ".1"],
164
+ [" .0", "5. ", "12. ", "1234.5", "1234567.9", "-5. ", "-1234.5", " .5", " .1"],
165
+ ["abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc"],
166
+ ["a0b", "a5b", "a12b", "a1235b", "a1234568b", "a-5b", "a-1235b", "a1b", "a0b"],
167
+ ["0 a", "5 a", "12 a", "1235 a", "1234568 a", "-5 a", "-1235 a", "1 a", "0 a"],
168
+ ["*0", "*5", "*12", "*1235", "*1234568", "*-5", "*-1235", "*1", "*0"],
169
+ ["(0)", "(5)", "(12)", "(1235)", "(1234568)", "(-5)", "(-1235)", "(1)", "(0)"],
170
+ [" 0", " 5", " 12", "1,235", "1,234,568", "( 5)", "(1,235)", " 1", " 0"]
171
+ ]
172
+ }
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Picture commands (10.8, ADR-0006): inserting an inline picture (its image a new resource of
3
+ * the envelope; alt text required, or the picture marked decorative), and changing a picture's
4
+ * size and alt text. Inserting and deleting a picture is tracked like text while tracking; its
5
+ * size does not change while tracking (Foli does not record it yet); alt text, which Word does
6
+ * not track either, changes any time.
7
+ */
8
+ import type { Node } from "../model/index.js";
9
+ import { TransformError } from "../transform/index.js";
10
+ import type { ResourceJSON } from "../v3/document.js";
11
+ import { checkResource } from "../v3/document.js";
12
+ import { attempt } from "./attempt.js";
13
+ import { deleteSelectionKeepingTables } from "./commands.js";
14
+ import { closeHistory } from "./history.js";
15
+ import { typedMarks } from "./mark.js";
16
+ import { marksAt, NodeSelection, TextSelection } from "./selection.js";
17
+ import type { Command, EditorState } from "./state.js";
18
+ import { trackState, untracked } from "./track.js";
19
+ import { withEnvelope, wordContext } from "./word.js";
20
+
21
+ /** The largest size the schema takes, in EMU. */
22
+ const MAX_EMU = 2_147_483_647;
23
+
24
+ /** A picture to insert: its image (a `data:` URL or a URL the app gave) and its size in EMU. */
25
+ export interface NewPicture {
26
+ /** The image's media type (`image/png`…). */
27
+ contentType: string;
28
+ /** Where the image is: a URL the app gave (an upload's) … */
29
+ url?: string;
30
+ /** … or, opted into, a `data:` URL holding it. */
31
+ data?: string;
32
+ width: number;
33
+ height: number;
34
+ /** Required unless `decorative`. */
35
+ alt?: string;
36
+ title?: string;
37
+ /** Marked decorative: no alt text needed (a screen reader passes it by). */
38
+ decorative?: boolean;
39
+ }
40
+
41
+ const validSize = (n: number) => Number.isInteger(n) && n >= 1 && n <= MAX_EMU;
42
+ /** Alt text and titles as the schema holds them. */
43
+ const validText = (value: unknown) =>
44
+ value === undefined || (typeof value === "string" && value.length <= 65_535);
45
+ /** A `data:` URL's media type, lower case. */
46
+ const dataType = (data: string) => /^data:([^;,]*)/i.exec(data)?.[1]?.toLowerCase();
47
+
48
+ /**
49
+ * Inserts `picture` at the selection (replacing it), its image added to the document's
50
+ * resources under a new key. Does not apply without alt text (unless decorative, or
51
+ * `requireAlt` is false: a picture pasted or dropped, asked for later and marked), with an image
52
+ * the document cannot hold (not an image type, no URL or `data:` URL, an unsafe URL), or where
53
+ * the selection is not in text.
54
+ */
55
+ export function insertPicture(
56
+ picture: NewPicture,
57
+ { requireAlt = true }: { requireAlt?: boolean } = {},
58
+ ): Command {
59
+ return (state, dispatch) => {
60
+ const context = wordContext(state);
61
+ const type = state.schema.nodes.picture;
62
+ if (!context || !type) return false;
63
+ if (!validText(picture.alt) || !validText(picture.title)) return false;
64
+ const alt = picture.decorative ? "" : (picture.alt?.trim() ?? "");
65
+ if (!alt && !picture.decorative && requireAlt) return false;
66
+ if (!validSize(picture.width) || !validSize(picture.height)) return false;
67
+ // The bytes are of the type the document says (a part is named from it on export).
68
+ if (
69
+ picture.data !== undefined &&
70
+ dataType(String(picture.data)) !== String(picture.contentType).toLowerCase()
71
+ )
72
+ return false;
73
+ const resource: ResourceJSON = {
74
+ contentType: picture.contentType,
75
+ ...(picture.url !== undefined ? { url: picture.url } : {}),
76
+ ...(picture.data !== undefined ? { data: picture.data } : {}),
77
+ };
78
+ if (checkResource(resource) !== undefined) return false;
79
+ const resources = context.envelope.resources ?? {};
80
+ let n = Object.keys(resources).length + 1;
81
+ while (Object.hasOwn(resources, `foli-picture-${n}`)) n++;
82
+ const key = `foli-picture-${n}`;
83
+ const node = type.create({
84
+ resource: key,
85
+ width: picture.width,
86
+ height: picture.height,
87
+ alt: alt || null,
88
+ title: picture.title || null,
89
+ decorative: picture.decorative || null,
90
+ });
91
+ return attempt(state, dispatch, (tr) => {
92
+ tr.setMeta(closeHistory, true);
93
+ // Over selected cells, into the anchor cell (as typing goes).
94
+ if (!tr.selection.empty) deleteSelectionKeepingTables(tr, true);
95
+ const $pos = tr.selection.$from;
96
+ if (!$pos.parent.inlineContent || !$pos.parent.type.contentMatch.matchType(type))
97
+ throw new TransformError("No text where the picture would go");
98
+ // The marks typing would get there (its run's properties, a link), never a tracked
99
+ // change's: tracking marks it as its own.
100
+ const marks = typedMarks(state.storedMarks ?? marksAt($pos)).filter((m) =>
101
+ $pos.parent.type.allowsMarkType(m.type),
102
+ );
103
+ tr.insert($pos.pos, node.mark(marks));
104
+ withEnvelope(tr, { ...context.envelope, resources: { ...resources, [key]: resource } });
105
+ tr.setSelection(TextSelection.create(tr.doc, $pos.pos + 1));
106
+ });
107
+ };
108
+ }
109
+
110
+ /** The selected picture (a node selection of one) and its position, if any. */
111
+ export function selectedPicture(state: EditorState): { node: Node; pos: number } | null {
112
+ const { selection } = state;
113
+ return selection instanceof NodeSelection && selection.node.type.name === "picture"
114
+ ? { node: selection.node, pos: selection.from }
115
+ : null;
116
+ }
117
+
118
+ /** The picture at `pos`, if there is one. */
119
+ const pictureAt = (state: EditorState, pos: number) => {
120
+ const node = pos >= 0 && pos < state.doc.content.size ? state.doc.nodeAt(pos) : null;
121
+ return node?.type.name === "picture" ? node : null;
122
+ };
123
+
124
+ /**
125
+ * Sets the size (EMU) of the picture at `pos`. Not while tracking (Foli does not record a
126
+ * picture's size change yet).
127
+ */
128
+ export function setPictureSize(pos: number, width: number, height: number): Command {
129
+ return (state, dispatch) => {
130
+ const node = pictureAt(state, pos);
131
+ if (!node || trackState(state)?.on === true) return false;
132
+ if (!validSize(width) || !validSize(height)) return false;
133
+ if (node.attrs.width === width && node.attrs.height === height) return false;
134
+ return attempt(state, dispatch, (tr) => {
135
+ tr.setMeta(closeHistory, true);
136
+ tr.setNodeMarkup(pos, node.type, { ...node.attrs, width, height });
137
+ // The picture stays selected (a resize keeps working on it).
138
+ if (state.selection instanceof NodeSelection && state.selection.from === pos)
139
+ tr.setSelection(NodeSelection.create(tr.doc, pos));
140
+ });
141
+ };
142
+ }
143
+
144
+ /**
145
+ * Sets the alt text of the picture at `pos`, or marks it decorative (no alt text). Does not
146
+ * apply with empty alt text unless decorative.
147
+ */
148
+ export function setPictureAlt(pos: number, alt: string, decorative = false): Command {
149
+ return (state, dispatch) => {
150
+ const node = pictureAt(state, pos);
151
+ if (!node || !validText(alt)) return false;
152
+ // A decorative picture has no alt text (a screen reader passes it by).
153
+ const text = decorative ? null : alt.trim() || null;
154
+ if (!text && !decorative) return false;
155
+ if (node.attrs.alt === text && (node.attrs.decorative === true) === decorative) return false;
156
+ return attempt(state, dispatch, (tr) => {
157
+ // Not a tracked change, as in Word.
158
+ tr.setMeta(closeHistory, true).setMeta(untracked, true);
159
+ tr.setNodeMarkup(pos, node.type, {
160
+ ...node.attrs,
161
+ alt: text,
162
+ decorative: decorative || null,
163
+ });
164
+ if (state.selection instanceof NodeSelection && state.selection.from === pos)
165
+ tr.setSelection(NodeSelection.create(tr.doc, pos));
166
+ });
167
+ };
168
+ }