@ckeditor/ckeditor5-heading 0.0.0-internal-20241017.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 (363) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +17 -0
  3. package/README.md +26 -0
  4. package/build/heading.js +5 -0
  5. package/build/translations/ar.js +1 -0
  6. package/build/translations/az.js +1 -0
  7. package/build/translations/bg.js +1 -0
  8. package/build/translations/bn.js +1 -0
  9. package/build/translations/bs.js +1 -0
  10. package/build/translations/ca.js +1 -0
  11. package/build/translations/cs.js +1 -0
  12. package/build/translations/da.js +1 -0
  13. package/build/translations/de-ch.js +1 -0
  14. package/build/translations/de.js +1 -0
  15. package/build/translations/el.js +1 -0
  16. package/build/translations/en-au.js +1 -0
  17. package/build/translations/en-gb.js +1 -0
  18. package/build/translations/eo.js +1 -0
  19. package/build/translations/es.js +1 -0
  20. package/build/translations/et.js +1 -0
  21. package/build/translations/eu.js +1 -0
  22. package/build/translations/fa.js +1 -0
  23. package/build/translations/fi.js +1 -0
  24. package/build/translations/fr.js +1 -0
  25. package/build/translations/gl.js +1 -0
  26. package/build/translations/he.js +1 -0
  27. package/build/translations/hi.js +1 -0
  28. package/build/translations/hr.js +1 -0
  29. package/build/translations/hu.js +1 -0
  30. package/build/translations/hy.js +1 -0
  31. package/build/translations/id.js +1 -0
  32. package/build/translations/it.js +1 -0
  33. package/build/translations/ja.js +1 -0
  34. package/build/translations/jv.js +1 -0
  35. package/build/translations/km.js +1 -0
  36. package/build/translations/kn.js +1 -0
  37. package/build/translations/ko.js +1 -0
  38. package/build/translations/ku.js +1 -0
  39. package/build/translations/lt.js +1 -0
  40. package/build/translations/lv.js +1 -0
  41. package/build/translations/ms.js +1 -0
  42. package/build/translations/nb.js +1 -0
  43. package/build/translations/ne.js +1 -0
  44. package/build/translations/nl.js +1 -0
  45. package/build/translations/no.js +1 -0
  46. package/build/translations/pl.js +1 -0
  47. package/build/translations/pt-br.js +1 -0
  48. package/build/translations/pt.js +1 -0
  49. package/build/translations/ro.js +1 -0
  50. package/build/translations/ru.js +1 -0
  51. package/build/translations/sk.js +1 -0
  52. package/build/translations/sl.js +1 -0
  53. package/build/translations/sq.js +1 -0
  54. package/build/translations/sr-latn.js +1 -0
  55. package/build/translations/sr.js +1 -0
  56. package/build/translations/sv.js +1 -0
  57. package/build/translations/th.js +1 -0
  58. package/build/translations/ti.js +1 -0
  59. package/build/translations/tk.js +1 -0
  60. package/build/translations/tr.js +1 -0
  61. package/build/translations/ug.js +1 -0
  62. package/build/translations/uk.js +1 -0
  63. package/build/translations/ur.js +1 -0
  64. package/build/translations/uz.js +1 -0
  65. package/build/translations/vi.js +1 -0
  66. package/build/translations/zh-cn.js +1 -0
  67. package/build/translations/zh.js +1 -0
  68. package/ckeditor5-metadata.json +46 -0
  69. package/dist/augmentation.d.ts +34 -0
  70. package/dist/heading.d.ts +40 -0
  71. package/dist/headingbuttonsui.d.ts +55 -0
  72. package/dist/headingcommand.d.ts +52 -0
  73. package/dist/headingconfig.d.ts +123 -0
  74. package/dist/headingediting.d.ts +50 -0
  75. package/dist/headingui.d.ts +30 -0
  76. package/dist/index-content.css +4 -0
  77. package/dist/index-editor.css +19 -0
  78. package/dist/index.css +26 -0
  79. package/dist/index.css.map +1 -0
  80. package/dist/index.d.ts +20 -0
  81. package/dist/index.js +937 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/title.d.ts +123 -0
  84. package/dist/translations/ar.d.ts +8 -0
  85. package/dist/translations/ar.js +5 -0
  86. package/dist/translations/ar.umd.js +11 -0
  87. package/dist/translations/az.d.ts +8 -0
  88. package/dist/translations/az.js +5 -0
  89. package/dist/translations/az.umd.js +11 -0
  90. package/dist/translations/bg.d.ts +8 -0
  91. package/dist/translations/bg.js +5 -0
  92. package/dist/translations/bg.umd.js +11 -0
  93. package/dist/translations/bn.d.ts +8 -0
  94. package/dist/translations/bn.js +5 -0
  95. package/dist/translations/bn.umd.js +11 -0
  96. package/dist/translations/bs.d.ts +8 -0
  97. package/dist/translations/bs.js +5 -0
  98. package/dist/translations/bs.umd.js +11 -0
  99. package/dist/translations/ca.d.ts +8 -0
  100. package/dist/translations/ca.js +5 -0
  101. package/dist/translations/ca.umd.js +11 -0
  102. package/dist/translations/cs.d.ts +8 -0
  103. package/dist/translations/cs.js +5 -0
  104. package/dist/translations/cs.umd.js +11 -0
  105. package/dist/translations/da.d.ts +8 -0
  106. package/dist/translations/da.js +5 -0
  107. package/dist/translations/da.umd.js +11 -0
  108. package/dist/translations/de-ch.d.ts +8 -0
  109. package/dist/translations/de-ch.js +5 -0
  110. package/dist/translations/de-ch.umd.js +11 -0
  111. package/dist/translations/de.d.ts +8 -0
  112. package/dist/translations/de.js +5 -0
  113. package/dist/translations/de.umd.js +11 -0
  114. package/dist/translations/el.d.ts +8 -0
  115. package/dist/translations/el.js +5 -0
  116. package/dist/translations/el.umd.js +11 -0
  117. package/dist/translations/en-au.d.ts +8 -0
  118. package/dist/translations/en-au.js +5 -0
  119. package/dist/translations/en-au.umd.js +11 -0
  120. package/dist/translations/en-gb.d.ts +8 -0
  121. package/dist/translations/en-gb.js +5 -0
  122. package/dist/translations/en-gb.umd.js +11 -0
  123. package/dist/translations/en.d.ts +8 -0
  124. package/dist/translations/en.js +5 -0
  125. package/dist/translations/en.umd.js +11 -0
  126. package/dist/translations/eo.d.ts +8 -0
  127. package/dist/translations/eo.js +5 -0
  128. package/dist/translations/eo.umd.js +11 -0
  129. package/dist/translations/es.d.ts +8 -0
  130. package/dist/translations/es.js +5 -0
  131. package/dist/translations/es.umd.js +11 -0
  132. package/dist/translations/et.d.ts +8 -0
  133. package/dist/translations/et.js +5 -0
  134. package/dist/translations/et.umd.js +11 -0
  135. package/dist/translations/eu.d.ts +8 -0
  136. package/dist/translations/eu.js +5 -0
  137. package/dist/translations/eu.umd.js +11 -0
  138. package/dist/translations/fa.d.ts +8 -0
  139. package/dist/translations/fa.js +5 -0
  140. package/dist/translations/fa.umd.js +11 -0
  141. package/dist/translations/fi.d.ts +8 -0
  142. package/dist/translations/fi.js +5 -0
  143. package/dist/translations/fi.umd.js +11 -0
  144. package/dist/translations/fr.d.ts +8 -0
  145. package/dist/translations/fr.js +5 -0
  146. package/dist/translations/fr.umd.js +11 -0
  147. package/dist/translations/gl.d.ts +8 -0
  148. package/dist/translations/gl.js +5 -0
  149. package/dist/translations/gl.umd.js +11 -0
  150. package/dist/translations/he.d.ts +8 -0
  151. package/dist/translations/he.js +5 -0
  152. package/dist/translations/he.umd.js +11 -0
  153. package/dist/translations/hi.d.ts +8 -0
  154. package/dist/translations/hi.js +5 -0
  155. package/dist/translations/hi.umd.js +11 -0
  156. package/dist/translations/hr.d.ts +8 -0
  157. package/dist/translations/hr.js +5 -0
  158. package/dist/translations/hr.umd.js +11 -0
  159. package/dist/translations/hu.d.ts +8 -0
  160. package/dist/translations/hu.js +5 -0
  161. package/dist/translations/hu.umd.js +11 -0
  162. package/dist/translations/hy.d.ts +8 -0
  163. package/dist/translations/hy.js +5 -0
  164. package/dist/translations/hy.umd.js +11 -0
  165. package/dist/translations/id.d.ts +8 -0
  166. package/dist/translations/id.js +5 -0
  167. package/dist/translations/id.umd.js +11 -0
  168. package/dist/translations/it.d.ts +8 -0
  169. package/dist/translations/it.js +5 -0
  170. package/dist/translations/it.umd.js +11 -0
  171. package/dist/translations/ja.d.ts +8 -0
  172. package/dist/translations/ja.js +5 -0
  173. package/dist/translations/ja.umd.js +11 -0
  174. package/dist/translations/jv.d.ts +8 -0
  175. package/dist/translations/jv.js +5 -0
  176. package/dist/translations/jv.umd.js +11 -0
  177. package/dist/translations/km.d.ts +8 -0
  178. package/dist/translations/km.js +5 -0
  179. package/dist/translations/km.umd.js +11 -0
  180. package/dist/translations/kn.d.ts +8 -0
  181. package/dist/translations/kn.js +5 -0
  182. package/dist/translations/kn.umd.js +11 -0
  183. package/dist/translations/ko.d.ts +8 -0
  184. package/dist/translations/ko.js +5 -0
  185. package/dist/translations/ko.umd.js +11 -0
  186. package/dist/translations/ku.d.ts +8 -0
  187. package/dist/translations/ku.js +5 -0
  188. package/dist/translations/ku.umd.js +11 -0
  189. package/dist/translations/lt.d.ts +8 -0
  190. package/dist/translations/lt.js +5 -0
  191. package/dist/translations/lt.umd.js +11 -0
  192. package/dist/translations/lv.d.ts +8 -0
  193. package/dist/translations/lv.js +5 -0
  194. package/dist/translations/lv.umd.js +11 -0
  195. package/dist/translations/ms.d.ts +8 -0
  196. package/dist/translations/ms.js +5 -0
  197. package/dist/translations/ms.umd.js +11 -0
  198. package/dist/translations/nb.d.ts +8 -0
  199. package/dist/translations/nb.js +5 -0
  200. package/dist/translations/nb.umd.js +11 -0
  201. package/dist/translations/ne.d.ts +8 -0
  202. package/dist/translations/ne.js +5 -0
  203. package/dist/translations/ne.umd.js +11 -0
  204. package/dist/translations/nl.d.ts +8 -0
  205. package/dist/translations/nl.js +5 -0
  206. package/dist/translations/nl.umd.js +11 -0
  207. package/dist/translations/no.d.ts +8 -0
  208. package/dist/translations/no.js +5 -0
  209. package/dist/translations/no.umd.js +11 -0
  210. package/dist/translations/pl.d.ts +8 -0
  211. package/dist/translations/pl.js +5 -0
  212. package/dist/translations/pl.umd.js +11 -0
  213. package/dist/translations/pt-br.d.ts +8 -0
  214. package/dist/translations/pt-br.js +5 -0
  215. package/dist/translations/pt-br.umd.js +11 -0
  216. package/dist/translations/pt.d.ts +8 -0
  217. package/dist/translations/pt.js +5 -0
  218. package/dist/translations/pt.umd.js +11 -0
  219. package/dist/translations/ro.d.ts +8 -0
  220. package/dist/translations/ro.js +5 -0
  221. package/dist/translations/ro.umd.js +11 -0
  222. package/dist/translations/ru.d.ts +8 -0
  223. package/dist/translations/ru.js +5 -0
  224. package/dist/translations/ru.umd.js +11 -0
  225. package/dist/translations/sk.d.ts +8 -0
  226. package/dist/translations/sk.js +5 -0
  227. package/dist/translations/sk.umd.js +11 -0
  228. package/dist/translations/sl.d.ts +8 -0
  229. package/dist/translations/sl.js +5 -0
  230. package/dist/translations/sl.umd.js +11 -0
  231. package/dist/translations/sq.d.ts +8 -0
  232. package/dist/translations/sq.js +5 -0
  233. package/dist/translations/sq.umd.js +11 -0
  234. package/dist/translations/sr-latn.d.ts +8 -0
  235. package/dist/translations/sr-latn.js +5 -0
  236. package/dist/translations/sr-latn.umd.js +11 -0
  237. package/dist/translations/sr.d.ts +8 -0
  238. package/dist/translations/sr.js +5 -0
  239. package/dist/translations/sr.umd.js +11 -0
  240. package/dist/translations/sv.d.ts +8 -0
  241. package/dist/translations/sv.js +5 -0
  242. package/dist/translations/sv.umd.js +11 -0
  243. package/dist/translations/th.d.ts +8 -0
  244. package/dist/translations/th.js +5 -0
  245. package/dist/translations/th.umd.js +11 -0
  246. package/dist/translations/ti.d.ts +8 -0
  247. package/dist/translations/ti.js +5 -0
  248. package/dist/translations/ti.umd.js +11 -0
  249. package/dist/translations/tk.d.ts +8 -0
  250. package/dist/translations/tk.js +5 -0
  251. package/dist/translations/tk.umd.js +11 -0
  252. package/dist/translations/tr.d.ts +8 -0
  253. package/dist/translations/tr.js +5 -0
  254. package/dist/translations/tr.umd.js +11 -0
  255. package/dist/translations/ug.d.ts +8 -0
  256. package/dist/translations/ug.js +5 -0
  257. package/dist/translations/ug.umd.js +11 -0
  258. package/dist/translations/uk.d.ts +8 -0
  259. package/dist/translations/uk.js +5 -0
  260. package/dist/translations/uk.umd.js +11 -0
  261. package/dist/translations/ur.d.ts +8 -0
  262. package/dist/translations/ur.js +5 -0
  263. package/dist/translations/ur.umd.js +11 -0
  264. package/dist/translations/uz.d.ts +8 -0
  265. package/dist/translations/uz.js +5 -0
  266. package/dist/translations/uz.umd.js +11 -0
  267. package/dist/translations/vi.d.ts +8 -0
  268. package/dist/translations/vi.js +5 -0
  269. package/dist/translations/vi.umd.js +11 -0
  270. package/dist/translations/zh-cn.d.ts +8 -0
  271. package/dist/translations/zh-cn.js +5 -0
  272. package/dist/translations/zh-cn.umd.js +11 -0
  273. package/dist/translations/zh.d.ts +8 -0
  274. package/dist/translations/zh.js +5 -0
  275. package/dist/translations/zh.umd.js +11 -0
  276. package/dist/utils.d.ts +22 -0
  277. package/lang/contexts.json +13 -0
  278. package/lang/translations/ar.po +62 -0
  279. package/lang/translations/az.po +62 -0
  280. package/lang/translations/bg.po +62 -0
  281. package/lang/translations/bn.po +62 -0
  282. package/lang/translations/bs.po +62 -0
  283. package/lang/translations/ca.po +62 -0
  284. package/lang/translations/cs.po +62 -0
  285. package/lang/translations/da.po +62 -0
  286. package/lang/translations/de-ch.po +62 -0
  287. package/lang/translations/de.po +62 -0
  288. package/lang/translations/el.po +62 -0
  289. package/lang/translations/en-au.po +62 -0
  290. package/lang/translations/en-gb.po +62 -0
  291. package/lang/translations/en.po +62 -0
  292. package/lang/translations/eo.po +62 -0
  293. package/lang/translations/es.po +62 -0
  294. package/lang/translations/et.po +62 -0
  295. package/lang/translations/eu.po +62 -0
  296. package/lang/translations/fa.po +62 -0
  297. package/lang/translations/fi.po +62 -0
  298. package/lang/translations/fr.po +62 -0
  299. package/lang/translations/gl.po +62 -0
  300. package/lang/translations/he.po +62 -0
  301. package/lang/translations/hi.po +62 -0
  302. package/lang/translations/hr.po +62 -0
  303. package/lang/translations/hu.po +62 -0
  304. package/lang/translations/hy.po +62 -0
  305. package/lang/translations/id.po +62 -0
  306. package/lang/translations/it.po +62 -0
  307. package/lang/translations/ja.po +62 -0
  308. package/lang/translations/jv.po +62 -0
  309. package/lang/translations/km.po +62 -0
  310. package/lang/translations/kn.po +62 -0
  311. package/lang/translations/ko.po +62 -0
  312. package/lang/translations/ku.po +62 -0
  313. package/lang/translations/lt.po +62 -0
  314. package/lang/translations/lv.po +62 -0
  315. package/lang/translations/ms.po +62 -0
  316. package/lang/translations/nb.po +62 -0
  317. package/lang/translations/ne.po +62 -0
  318. package/lang/translations/nl.po +62 -0
  319. package/lang/translations/no.po +62 -0
  320. package/lang/translations/pl.po +62 -0
  321. package/lang/translations/pt-br.po +62 -0
  322. package/lang/translations/pt.po +62 -0
  323. package/lang/translations/ro.po +62 -0
  324. package/lang/translations/ru.po +62 -0
  325. package/lang/translations/sk.po +62 -0
  326. package/lang/translations/sl.po +62 -0
  327. package/lang/translations/sq.po +62 -0
  328. package/lang/translations/sr-latn.po +62 -0
  329. package/lang/translations/sr.po +62 -0
  330. package/lang/translations/sv.po +62 -0
  331. package/lang/translations/th.po +62 -0
  332. package/lang/translations/ti.po +62 -0
  333. package/lang/translations/tk.po +62 -0
  334. package/lang/translations/tr.po +62 -0
  335. package/lang/translations/ug.po +62 -0
  336. package/lang/translations/uk.po +62 -0
  337. package/lang/translations/ur.po +62 -0
  338. package/lang/translations/uz.po +62 -0
  339. package/lang/translations/vi.po +62 -0
  340. package/lang/translations/zh-cn.po +62 -0
  341. package/lang/translations/zh.po +62 -0
  342. package/package.json +43 -0
  343. package/src/augmentation.d.ts +30 -0
  344. package/src/augmentation.js +5 -0
  345. package/src/heading.d.ts +36 -0
  346. package/src/heading.js +42 -0
  347. package/src/headingbuttonsui.d.ts +51 -0
  348. package/src/headingbuttonsui.js +83 -0
  349. package/src/headingcommand.d.ts +48 -0
  350. package/src/headingcommand.js +65 -0
  351. package/src/headingconfig.d.ts +119 -0
  352. package/src/headingconfig.js +5 -0
  353. package/src/headingediting.d.ts +46 -0
  354. package/src/headingediting.js +107 -0
  355. package/src/headingui.d.ts +26 -0
  356. package/src/headingui.js +171 -0
  357. package/src/index.d.ts +16 -0
  358. package/src/index.js +13 -0
  359. package/src/title.d.ts +119 -0
  360. package/src/title.js +466 -0
  361. package/src/utils.d.ts +18 -0
  362. package/src/utils.js +31 -0
  363. package/theme/heading.css +20 -0
package/dist/index.js ADDED
@@ -0,0 +1,937 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import { Command, Plugin, icons } from '@ckeditor/ckeditor5-core/dist/index.js';
6
+ import { Paragraph } from '@ckeditor/ckeditor5-paragraph/dist/index.js';
7
+ import { first, priorities, Collection } from '@ckeditor/ckeditor5-utils/dist/index.js';
8
+ import { ViewModel, createDropdown, addListToDropdown, MenuBarMenuView, MenuBarMenuListView, MenuBarMenuListItemView, MenuBarMenuListItemButtonView, ButtonView } from '@ckeditor/ckeditor5-ui/dist/index.js';
9
+ import { DowncastWriter, enablePlaceholder, hidePlaceholder, needsPlaceholder, showPlaceholder } from '@ckeditor/ckeditor5-engine/dist/index.js';
10
+
11
+ /**
12
+ * The heading command. It is used by the {@link module:heading/heading~Heading heading feature} to apply headings.
13
+ */ class HeadingCommand extends Command {
14
+ /**
15
+ * Set of defined model's elements names that this command support.
16
+ * See {@link module:heading/headingconfig~HeadingOption}.
17
+ */ modelElements;
18
+ /**
19
+ * Creates an instance of the command.
20
+ *
21
+ * @param editor Editor instance.
22
+ * @param modelElements Names of the element which this command can apply in the model.
23
+ */ constructor(editor, modelElements){
24
+ super(editor);
25
+ this.modelElements = modelElements;
26
+ }
27
+ /**
28
+ * @inheritDoc
29
+ */ refresh() {
30
+ const block = first(this.editor.model.document.selection.getSelectedBlocks());
31
+ this.value = !!block && this.modelElements.includes(block.name) && block.name;
32
+ this.isEnabled = !!block && this.modelElements.some((heading)=>checkCanBecomeHeading(block, heading, this.editor.model.schema));
33
+ }
34
+ /**
35
+ * Executes the command. Applies the heading to the selected blocks or, if the first selected
36
+ * block is a heading already, turns selected headings (of this level only) to paragraphs.
37
+ *
38
+ * @param options.value Name of the element which this command will apply in the model.
39
+ * @fires execute
40
+ */ execute(options) {
41
+ const model = this.editor.model;
42
+ const document = model.document;
43
+ const modelElement = options.value;
44
+ model.change((writer)=>{
45
+ const blocks = Array.from(document.selection.getSelectedBlocks()).filter((block)=>{
46
+ return checkCanBecomeHeading(block, modelElement, model.schema);
47
+ });
48
+ for (const block of blocks){
49
+ if (!block.is('element', modelElement)) {
50
+ writer.rename(block, modelElement);
51
+ }
52
+ }
53
+ });
54
+ }
55
+ }
56
+ /**
57
+ * Checks whether the given block can be replaced by a specific heading.
58
+ *
59
+ * @param block A block to be tested.
60
+ * @param heading Command element name in the model.
61
+ * @param schema The schema of the document.
62
+ */ function checkCanBecomeHeading(block, heading, schema) {
63
+ return schema.checkChild(block.parent, heading) && !schema.isObject(block);
64
+ }
65
+
66
+ const defaultModelElement = 'paragraph';
67
+ /**
68
+ * The headings engine feature. It handles switching between block formats – headings and paragraph.
69
+ * This class represents the engine part of the heading feature. See also {@link module:heading/heading~Heading}.
70
+ * It introduces `heading1`-`headingN` commands which allow to convert paragraphs into headings.
71
+ */ class HeadingEditing extends Plugin {
72
+ /**
73
+ * @inheritDoc
74
+ */ static get pluginName() {
75
+ return 'HeadingEditing';
76
+ }
77
+ /**
78
+ * @inheritDoc
79
+ */ static get isOfficialPlugin() {
80
+ return true;
81
+ }
82
+ /**
83
+ * @inheritDoc
84
+ */ constructor(editor){
85
+ super(editor);
86
+ editor.config.define('heading', {
87
+ options: [
88
+ {
89
+ model: 'paragraph',
90
+ title: 'Paragraph',
91
+ class: 'ck-heading_paragraph'
92
+ },
93
+ {
94
+ model: 'heading1',
95
+ view: 'h2',
96
+ title: 'Heading 1',
97
+ class: 'ck-heading_heading1'
98
+ },
99
+ {
100
+ model: 'heading2',
101
+ view: 'h3',
102
+ title: 'Heading 2',
103
+ class: 'ck-heading_heading2'
104
+ },
105
+ {
106
+ model: 'heading3',
107
+ view: 'h4',
108
+ title: 'Heading 3',
109
+ class: 'ck-heading_heading3'
110
+ }
111
+ ]
112
+ });
113
+ }
114
+ /**
115
+ * @inheritDoc
116
+ */ static get requires() {
117
+ return [
118
+ Paragraph
119
+ ];
120
+ }
121
+ /**
122
+ * @inheritDoc
123
+ */ init() {
124
+ const editor = this.editor;
125
+ const options = editor.config.get('heading.options');
126
+ const modelElements = [];
127
+ for (const option of options){
128
+ // Skip paragraph - it is defined in required Paragraph feature.
129
+ if (option.model === 'paragraph') {
130
+ continue;
131
+ }
132
+ // Schema.
133
+ editor.model.schema.register(option.model, {
134
+ inheritAllFrom: '$block'
135
+ });
136
+ editor.conversion.elementToElement(option);
137
+ modelElements.push(option.model);
138
+ }
139
+ this._addDefaultH1Conversion(editor);
140
+ // Register the heading command for this option.
141
+ editor.commands.add('heading', new HeadingCommand(editor, modelElements));
142
+ }
143
+ /**
144
+ * @inheritDoc
145
+ */ afterInit() {
146
+ // If the enter command is added to the editor, alter its behavior.
147
+ // Enter at the end of a heading element should create a paragraph.
148
+ const editor = this.editor;
149
+ const enterCommand = editor.commands.get('enter');
150
+ const options = editor.config.get('heading.options');
151
+ if (enterCommand) {
152
+ this.listenTo(enterCommand, 'afterExecute', (evt, data)=>{
153
+ const positionParent = editor.model.document.selection.getFirstPosition().parent;
154
+ const isHeading = options.some((option)=>positionParent.is('element', option.model));
155
+ if (isHeading && !positionParent.is('element', defaultModelElement) && positionParent.childCount === 0) {
156
+ data.writer.rename(positionParent, defaultModelElement);
157
+ }
158
+ });
159
+ }
160
+ }
161
+ /**
162
+ * Adds default conversion for `h1` -> `heading1` with a low priority.
163
+ *
164
+ * @param editor Editor instance on which to add the `h1` conversion.
165
+ */ _addDefaultH1Conversion(editor) {
166
+ editor.conversion.for('upcast').elementToElement({
167
+ model: 'heading1',
168
+ view: 'h1',
169
+ // With a `low` priority, `paragraph` plugin autoparagraphing mechanism is executed. Make sure
170
+ // this listener is called before it. If not, `h1` will be transformed into a paragraph.
171
+ converterPriority: priorities.low + 1
172
+ });
173
+ }
174
+ }
175
+
176
+ /**
177
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
178
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
179
+ */ /**
180
+ * @module heading/utils
181
+ */ /**
182
+ * Returns heading options as defined in `config.heading.options` but processed to consider
183
+ * the editor localization, i.e. to display {@link module:heading/headingconfig~HeadingOption}
184
+ * in the correct language.
185
+ *
186
+ * Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
187
+ * when the user configuration is defined because the editor does not exist yet.
188
+ */ function getLocalizedOptions(editor) {
189
+ const t = editor.t;
190
+ const localizedTitles = {
191
+ 'Paragraph': t('Paragraph'),
192
+ 'Heading 1': t('Heading 1'),
193
+ 'Heading 2': t('Heading 2'),
194
+ 'Heading 3': t('Heading 3'),
195
+ 'Heading 4': t('Heading 4'),
196
+ 'Heading 5': t('Heading 5'),
197
+ 'Heading 6': t('Heading 6')
198
+ };
199
+ return editor.config.get('heading.options').map((option)=>{
200
+ const title = localizedTitles[option.title];
201
+ if (title && title != option.title) {
202
+ option.title = title;
203
+ }
204
+ return option;
205
+ });
206
+ }
207
+
208
+ /**
209
+ * The headings UI feature. It introduces the `headings` dropdown.
210
+ */ class HeadingUI extends Plugin {
211
+ /**
212
+ * @inheritDoc
213
+ */ static get pluginName() {
214
+ return 'HeadingUI';
215
+ }
216
+ /**
217
+ * @inheritDoc
218
+ */ static get isOfficialPlugin() {
219
+ return true;
220
+ }
221
+ /**
222
+ * @inheritDoc
223
+ */ init() {
224
+ const editor = this.editor;
225
+ const t = editor.t;
226
+ const options = getLocalizedOptions(editor);
227
+ const defaultTitle = t('Choose heading');
228
+ const accessibleLabel = t('Heading');
229
+ // Register UI component.
230
+ editor.ui.componentFactory.add('heading', (locale)=>{
231
+ const titles = {};
232
+ const itemDefinitions = new Collection();
233
+ const headingCommand = editor.commands.get('heading');
234
+ const paragraphCommand = editor.commands.get('paragraph');
235
+ const commands = [
236
+ headingCommand
237
+ ];
238
+ for (const option of options){
239
+ const def = {
240
+ type: 'button',
241
+ model: new ViewModel({
242
+ label: option.title,
243
+ class: option.class,
244
+ role: 'menuitemradio',
245
+ withText: true
246
+ })
247
+ };
248
+ if (option.model === 'paragraph') {
249
+ def.model.bind('isOn').to(paragraphCommand, 'value');
250
+ def.model.set('commandName', 'paragraph');
251
+ commands.push(paragraphCommand);
252
+ } else {
253
+ def.model.bind('isOn').to(headingCommand, 'value', (value)=>value === option.model);
254
+ def.model.set({
255
+ commandName: 'heading',
256
+ commandValue: option.model
257
+ });
258
+ }
259
+ // Add the option to the collection.
260
+ itemDefinitions.add(def);
261
+ titles[option.model] = option.title;
262
+ }
263
+ const dropdownView = createDropdown(locale);
264
+ addListToDropdown(dropdownView, itemDefinitions, {
265
+ ariaLabel: accessibleLabel,
266
+ role: 'menu'
267
+ });
268
+ dropdownView.buttonView.set({
269
+ ariaLabel: accessibleLabel,
270
+ ariaLabelledBy: undefined,
271
+ isOn: false,
272
+ withText: true,
273
+ tooltip: accessibleLabel
274
+ });
275
+ dropdownView.extendTemplate({
276
+ attributes: {
277
+ class: [
278
+ 'ck-heading-dropdown'
279
+ ]
280
+ }
281
+ });
282
+ dropdownView.bind('isEnabled').toMany(commands, 'isEnabled', (...areEnabled)=>{
283
+ return areEnabled.some((isEnabled)=>isEnabled);
284
+ });
285
+ dropdownView.buttonView.bind('label').to(headingCommand, 'value', paragraphCommand, 'value', (heading, paragraph)=>{
286
+ const whichModel = paragraph ? 'paragraph' : heading;
287
+ if (typeof whichModel === 'boolean') {
288
+ return defaultTitle;
289
+ }
290
+ // If none of the commands is active, display default title.
291
+ if (!titles[whichModel]) {
292
+ return defaultTitle;
293
+ }
294
+ return titles[whichModel];
295
+ });
296
+ dropdownView.buttonView.bind('ariaLabel').to(headingCommand, 'value', paragraphCommand, 'value', (heading, paragraph)=>{
297
+ const whichModel = paragraph ? 'paragraph' : heading;
298
+ if (typeof whichModel === 'boolean') {
299
+ return accessibleLabel;
300
+ }
301
+ // If none of the commands is active, display default title.
302
+ if (!titles[whichModel]) {
303
+ return accessibleLabel;
304
+ }
305
+ return `${titles[whichModel]}, ${accessibleLabel}`;
306
+ });
307
+ // Execute command when an item from the dropdown is selected.
308
+ this.listenTo(dropdownView, 'execute', (evt)=>{
309
+ const { commandName, commandValue } = evt.source;
310
+ editor.execute(commandName, commandValue ? {
311
+ value: commandValue
312
+ } : undefined);
313
+ editor.editing.view.focus();
314
+ });
315
+ return dropdownView;
316
+ });
317
+ editor.ui.componentFactory.add('menuBar:heading', (locale)=>{
318
+ const menuView = new MenuBarMenuView(locale);
319
+ const headingCommand = editor.commands.get('heading');
320
+ const paragraphCommand = editor.commands.get('paragraph');
321
+ const commands = [
322
+ headingCommand
323
+ ];
324
+ const listView = new MenuBarMenuListView(locale);
325
+ menuView.set({
326
+ class: 'ck-heading-dropdown'
327
+ });
328
+ listView.set({
329
+ ariaLabel: t('Heading'),
330
+ role: 'menu'
331
+ });
332
+ menuView.buttonView.set({
333
+ label: t('Heading')
334
+ });
335
+ menuView.panelView.children.add(listView);
336
+ for (const option of options){
337
+ const listItemView = new MenuBarMenuListItemView(locale, menuView);
338
+ const buttonView = new MenuBarMenuListItemButtonView(locale);
339
+ listItemView.children.add(buttonView);
340
+ listView.items.add(listItemView);
341
+ buttonView.set({
342
+ isToggleable: true,
343
+ label: option.title,
344
+ role: 'menuitemradio',
345
+ class: option.class
346
+ });
347
+ buttonView.delegate('execute').to(menuView);
348
+ buttonView.on('execute', ()=>{
349
+ const commandName = option.model === 'paragraph' ? 'paragraph' : 'heading';
350
+ editor.execute(commandName, {
351
+ value: option.model
352
+ });
353
+ editor.editing.view.focus();
354
+ });
355
+ if (option.model === 'paragraph') {
356
+ buttonView.bind('isOn').to(paragraphCommand, 'value');
357
+ commands.push(paragraphCommand);
358
+ } else {
359
+ buttonView.bind('isOn').to(headingCommand, 'value', (value)=>value === option.model);
360
+ }
361
+ }
362
+ menuView.bind('isEnabled').toMany(commands, 'isEnabled', (...areEnabled)=>{
363
+ return areEnabled.some((isEnabled)=>isEnabled);
364
+ });
365
+ return menuView;
366
+ });
367
+ }
368
+ }
369
+
370
+ /**
371
+ * The headings feature.
372
+ *
373
+ * For a detailed overview, check the {@glink features/headings Headings feature} guide
374
+ * and the {@glink api/heading package page}.
375
+ *
376
+ * This is a "glue" plugin which loads the {@link module:heading/headingediting~HeadingEditing heading editing feature}
377
+ * and {@link module:heading/headingui~HeadingUI heading UI feature}.
378
+ *
379
+ * @extends module:core/plugin~Plugin
380
+ */ class Heading extends Plugin {
381
+ /**
382
+ * @inheritDoc
383
+ */ static get requires() {
384
+ return [
385
+ HeadingEditing,
386
+ HeadingUI
387
+ ];
388
+ }
389
+ /**
390
+ * @inheritDoc
391
+ */ static get pluginName() {
392
+ return 'Heading';
393
+ }
394
+ /**
395
+ * @inheritDoc
396
+ */ static get isOfficialPlugin() {
397
+ return true;
398
+ }
399
+ }
400
+
401
+ const defaultIcons = /* #__PURE__ */ (()=>({
402
+ heading1: icons.heading1,
403
+ heading2: icons.heading2,
404
+ heading3: icons.heading3,
405
+ heading4: icons.heading4,
406
+ heading5: icons.heading5,
407
+ heading6: icons.heading6
408
+ }))();
409
+ /**
410
+ * The `HeadingButtonsUI` plugin defines a set of UI buttons that can be used instead of the
411
+ * standard drop down component.
412
+ *
413
+ * This feature is not enabled by default by the {@link module:heading/heading~Heading} plugin and needs to be
414
+ * installed manually to the editor configuration.
415
+ *
416
+ * Plugin introduces button UI elements, which names are same as `model` property from {@link module:heading/headingconfig~HeadingOption}.
417
+ *
418
+ * ```ts
419
+ * ClassicEditor
420
+ * .create( {
421
+ * plugins: [ ..., Heading, Paragraph, HeadingButtonsUI, ParagraphButtonUI ]
422
+ * heading: {
423
+ * options: [
424
+ * { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
425
+ * { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
426
+ * { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
427
+ * { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
428
+ * ]
429
+ * },
430
+ * toolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3' ]
431
+ * } )
432
+ * .then( ... )
433
+ * .catch( ... );
434
+ * ```
435
+ *
436
+ * NOTE: The `'paragraph'` button is defined in by the {@link module:paragraph/paragraphbuttonui~ParagraphButtonUI} plugin
437
+ * which needs to be loaded manually as well.
438
+ *
439
+ * It is possible to use custom icons by providing `icon` config option in {@link module:heading/headingconfig~HeadingOption}.
440
+ * For the default configuration standard icons are used.
441
+ */ class HeadingButtonsUI extends Plugin {
442
+ /**
443
+ * @inheritDoc
444
+ */ init() {
445
+ const options = getLocalizedOptions(this.editor);
446
+ options.filter((item)=>item.model !== 'paragraph').map((item)=>this._createButton(item));
447
+ }
448
+ /**
449
+ * Creates single button view from provided configuration option.
450
+ */ _createButton(option) {
451
+ const editor = this.editor;
452
+ editor.ui.componentFactory.add(option.model, (locale)=>{
453
+ const view = new ButtonView(locale);
454
+ const command = editor.commands.get('heading');
455
+ view.label = option.title;
456
+ view.icon = option.icon || defaultIcons[option.model];
457
+ view.tooltip = true;
458
+ view.isToggleable = true;
459
+ view.bind('isEnabled').to(command);
460
+ view.bind('isOn').to(command, 'value', (value)=>value == option.model);
461
+ view.on('execute', ()=>{
462
+ editor.execute('heading', {
463
+ value: option.model
464
+ });
465
+ editor.editing.view.focus();
466
+ });
467
+ return view;
468
+ });
469
+ }
470
+ }
471
+
472
+ // A list of element names that should be treated by the Title plugin as title-like.
473
+ // This means that an element of a type from this list will be changed to a title element
474
+ // when it is the first element in the root.
475
+ const titleLikeElements = new Set([
476
+ 'paragraph',
477
+ 'heading1',
478
+ 'heading2',
479
+ 'heading3',
480
+ 'heading4',
481
+ 'heading5',
482
+ 'heading6'
483
+ ]);
484
+ /**
485
+ * The Title plugin.
486
+ *
487
+ * It splits the document into `Title` and `Body` sections.
488
+ */ class Title extends Plugin {
489
+ /**
490
+ * A reference to an empty paragraph in the body
491
+ * created when there is no element in the body for the placeholder purposes.
492
+ */ _bodyPlaceholder = new Map();
493
+ /**
494
+ * @inheritDoc
495
+ */ static get pluginName() {
496
+ return 'Title';
497
+ }
498
+ /**
499
+ * @inheritDoc
500
+ */ static get isOfficialPlugin() {
501
+ return true;
502
+ }
503
+ /**
504
+ * @inheritDoc
505
+ */ static get requires() {
506
+ return [
507
+ 'Paragraph'
508
+ ];
509
+ }
510
+ /**
511
+ * @inheritDoc
512
+ */ init() {
513
+ const editor = this.editor;
514
+ const model = editor.model;
515
+ // To use the schema for disabling some features when the selection is inside the title element
516
+ // it is needed to create the following structure:
517
+ //
518
+ // <title>
519
+ // <title-content>The title text</title-content>
520
+ // </title>
521
+ //
522
+ // See: https://github.com/ckeditor/ckeditor5/issues/2005.
523
+ model.schema.register('title', {
524
+ isBlock: true,
525
+ allowIn: '$root'
526
+ });
527
+ model.schema.register('title-content', {
528
+ isBlock: true,
529
+ allowIn: 'title',
530
+ allowAttributes: [
531
+ 'alignment'
532
+ ]
533
+ });
534
+ model.schema.extend('$text', {
535
+ allowIn: 'title-content'
536
+ });
537
+ // Disallow all attributes in `title-content`.
538
+ model.schema.addAttributeCheck((context)=>{
539
+ if (context.endsWith('title-content $text')) {
540
+ return false;
541
+ }
542
+ });
543
+ // Because `title` is represented by two elements in the model
544
+ // but only one in the view, it is needed to adjust Mapper.
545
+ editor.editing.mapper.on('modelToViewPosition', mapModelPositionToView(editor.editing.view));
546
+ editor.data.mapper.on('modelToViewPosition', mapModelPositionToView(editor.editing.view));
547
+ // Conversion.
548
+ editor.conversion.for('downcast').elementToElement({
549
+ model: 'title-content',
550
+ view: 'h1'
551
+ });
552
+ editor.conversion.for('downcast').add((dispatcher)=>dispatcher.on('insert:title', (evt, data, conversionApi)=>{
553
+ conversionApi.consumable.consume(data.item, evt.name);
554
+ }));
555
+ // Custom converter is used for data v -> m conversion to avoid calling post-fixer when setting data.
556
+ // See https://github.com/ckeditor/ckeditor5/issues/2036.
557
+ editor.data.upcastDispatcher.on('element:h1', dataViewModelH1Insertion, {
558
+ priority: 'high'
559
+ });
560
+ editor.data.upcastDispatcher.on('element:h2', dataViewModelH1Insertion, {
561
+ priority: 'high'
562
+ });
563
+ editor.data.upcastDispatcher.on('element:h3', dataViewModelH1Insertion, {
564
+ priority: 'high'
565
+ });
566
+ // Take care about correct `title` element structure.
567
+ model.document.registerPostFixer((writer)=>this._fixTitleContent(writer));
568
+ // Create and take care of correct position of a `title` element.
569
+ model.document.registerPostFixer((writer)=>this._fixTitleElement(writer));
570
+ // Create element for `Body` placeholder if it is missing.
571
+ model.document.registerPostFixer((writer)=>this._fixBodyElement(writer));
572
+ // Prevent from adding extra at the end of the document.
573
+ model.document.registerPostFixer((writer)=>this._fixExtraParagraph(writer));
574
+ // Attach `Title` and `Body` placeholders to the empty title and/or content.
575
+ this._attachPlaceholders();
576
+ // Attach Tab handling.
577
+ this._attachTabPressHandling();
578
+ }
579
+ /**
580
+ * Returns the title of the document. Note that because this plugin does not allow any formatting inside
581
+ * the title element, the output of this method will be a plain text, with no HTML tags.
582
+ *
583
+ * It is not recommended to use this method together with features that insert markers to the
584
+ * data output, like comments or track changes features. If such markers start in the title and end in the
585
+ * body, the result of this method might be incorrect.
586
+ *
587
+ * @param options Additional configuration passed to the conversion process.
588
+ * See {@link module:engine/controller/datacontroller~DataController#get `DataController#get`}.
589
+ * @returns The title of the document.
590
+ */ getTitle(options = {}) {
591
+ const rootName = options.rootName ? options.rootName : undefined;
592
+ const titleElement = this._getTitleElement(rootName);
593
+ const titleContentElement = titleElement.getChild(0);
594
+ return this.editor.data.stringify(titleContentElement, options);
595
+ }
596
+ /**
597
+ * Returns the body of the document.
598
+ *
599
+ * Note that it is not recommended to use this method together with features that insert markers to the
600
+ * data output, like comments or track changes features. If such markers start in the title and end in the
601
+ * body, the result of this method might be incorrect.
602
+ *
603
+ * @param options Additional configuration passed to the conversion process.
604
+ * See {@link module:engine/controller/datacontroller~DataController#get `DataController#get`}.
605
+ * @returns The body of the document.
606
+ */ getBody(options = {}) {
607
+ const editor = this.editor;
608
+ const data = editor.data;
609
+ const model = editor.model;
610
+ const rootName = options.rootName ? options.rootName : undefined;
611
+ const root = editor.model.document.getRoot(rootName);
612
+ const view = editor.editing.view;
613
+ const viewWriter = new DowncastWriter(view.document);
614
+ const rootRange = model.createRangeIn(root);
615
+ const viewDocumentFragment = viewWriter.createDocumentFragment();
616
+ // Find all markers that intersects with body.
617
+ const bodyStartPosition = model.createPositionAfter(root.getChild(0));
618
+ const bodyRange = model.createRange(bodyStartPosition, model.createPositionAt(root, 'end'));
619
+ const markers = new Map();
620
+ for (const marker of model.markers){
621
+ const intersection = bodyRange.getIntersection(marker.getRange());
622
+ if (intersection) {
623
+ markers.set(marker.name, intersection);
624
+ }
625
+ }
626
+ // Convert the entire root to view.
627
+ data.mapper.clearBindings();
628
+ data.mapper.bindElements(root, viewDocumentFragment);
629
+ data.downcastDispatcher.convert(rootRange, markers, viewWriter, options);
630
+ // Remove title element from view.
631
+ viewWriter.remove(viewWriter.createRangeOn(viewDocumentFragment.getChild(0)));
632
+ // view -> data
633
+ return editor.data.processor.toData(viewDocumentFragment);
634
+ }
635
+ /**
636
+ * Returns the `title` element when it is in the document. Returns `undefined` otherwise.
637
+ */ _getTitleElement(rootName) {
638
+ const root = this.editor.model.document.getRoot(rootName);
639
+ for (const child of root.getChildren()){
640
+ if (isTitle(child)) {
641
+ return child;
642
+ }
643
+ }
644
+ }
645
+ /**
646
+ * Model post-fixer callback that ensures that `title` has only one `title-content` child.
647
+ * All additional children should be moved after the `title` element and renamed to a paragraph.
648
+ */ _fixTitleContent(writer) {
649
+ let changed = false;
650
+ for (const rootName of this.editor.model.document.getRootNames()){
651
+ const title = this._getTitleElement(rootName);
652
+ // If there is no title in the content it will be created by `_fixTitleElement` post-fixer.
653
+ // If the title has just one element, then it is correct. No fixing.
654
+ if (!title || title.maxOffset === 1) {
655
+ continue;
656
+ }
657
+ const titleChildren = Array.from(title.getChildren());
658
+ // Skip first child because it is an allowed element.
659
+ titleChildren.shift();
660
+ for (const titleChild of titleChildren){
661
+ writer.move(writer.createRangeOn(titleChild), title, 'after');
662
+ writer.rename(titleChild, 'paragraph');
663
+ }
664
+ changed = true;
665
+ }
666
+ return changed;
667
+ }
668
+ /**
669
+ * Model post-fixer callback that creates a title element when it is missing,
670
+ * takes care of the correct position of it and removes additional title elements.
671
+ */ _fixTitleElement(writer) {
672
+ let changed = false;
673
+ const model = this.editor.model;
674
+ for (const modelRoot of this.editor.model.document.getRoots()){
675
+ const titleElements = Array.from(modelRoot.getChildren()).filter(isTitle);
676
+ const firstTitleElement = titleElements[0];
677
+ const firstRootChild = modelRoot.getChild(0);
678
+ // When title element is at the beginning of the document then try to fix additional title elements (if there are any).
679
+ if (firstRootChild.is('element', 'title')) {
680
+ if (titleElements.length > 1) {
681
+ fixAdditionalTitleElements(titleElements, writer, model);
682
+ changed = true;
683
+ }
684
+ continue;
685
+ }
686
+ // When there is no title in the document and first element in the document cannot be changed
687
+ // to the title then create an empty title element at the beginning of the document.
688
+ if (!firstTitleElement && !titleLikeElements.has(firstRootChild.name)) {
689
+ const title = writer.createElement('title');
690
+ writer.insert(title, modelRoot);
691
+ writer.insertElement('title-content', title);
692
+ changed = true;
693
+ continue;
694
+ }
695
+ if (titleLikeElements.has(firstRootChild.name)) {
696
+ // Change the first element in the document to the title if it can be changed (is title-like).
697
+ changeElementToTitle(firstRootChild, writer, model);
698
+ } else {
699
+ // Otherwise, move the first occurrence of the title element to the beginning of the document.
700
+ writer.move(writer.createRangeOn(firstTitleElement), modelRoot, 0);
701
+ }
702
+ fixAdditionalTitleElements(titleElements, writer, model);
703
+ changed = true;
704
+ }
705
+ return changed;
706
+ }
707
+ /**
708
+ * Model post-fixer callback that adds an empty paragraph at the end of the document
709
+ * when it is needed for the placeholder purposes.
710
+ */ _fixBodyElement(writer) {
711
+ let changed = false;
712
+ for (const rootName of this.editor.model.document.getRootNames()){
713
+ const modelRoot = this.editor.model.document.getRoot(rootName);
714
+ if (modelRoot.childCount < 2) {
715
+ const placeholder = writer.createElement('paragraph');
716
+ writer.insert(placeholder, modelRoot, 1);
717
+ this._bodyPlaceholder.set(rootName, placeholder);
718
+ changed = true;
719
+ }
720
+ }
721
+ return changed;
722
+ }
723
+ /**
724
+ * Model post-fixer callback that removes a paragraph from the end of the document
725
+ * if it was created for the placeholder purposes and is not needed anymore.
726
+ */ _fixExtraParagraph(writer) {
727
+ let changed = false;
728
+ for (const rootName of this.editor.model.document.getRootNames()){
729
+ const root = this.editor.model.document.getRoot(rootName);
730
+ const placeholder = this._bodyPlaceholder.get(rootName);
731
+ if (shouldRemoveLastParagraph(placeholder, root)) {
732
+ this._bodyPlaceholder.delete(rootName);
733
+ writer.remove(placeholder);
734
+ changed = true;
735
+ }
736
+ }
737
+ return changed;
738
+ }
739
+ /**
740
+ * Attaches the `Title` and `Body` placeholders to the title and/or content.
741
+ */ _attachPlaceholders() {
742
+ const editor = this.editor;
743
+ const t = editor.t;
744
+ const view = editor.editing.view;
745
+ const sourceElement = editor.sourceElement;
746
+ const titlePlaceholder = editor.config.get('title.placeholder') || t('Type your title');
747
+ const bodyPlaceholder = editor.config.get('placeholder') || sourceElement && sourceElement.tagName.toLowerCase() === 'textarea' && sourceElement.getAttribute('placeholder') || t('Type or paste your content here.');
748
+ // Attach placeholder to the view title element.
749
+ editor.editing.downcastDispatcher.on('insert:title-content', (evt, data, conversionApi)=>{
750
+ const element = conversionApi.mapper.toViewElement(data.item);
751
+ element.placeholder = titlePlaceholder;
752
+ enablePlaceholder({
753
+ view,
754
+ element,
755
+ keepOnFocus: true
756
+ });
757
+ });
758
+ // Attach placeholder to first element after a title element and remove it if it's not needed anymore.
759
+ // First element after title can change, so we need to observe all changes keep placeholder in sync.
760
+ const bodyViewElements = new Map();
761
+ // This post-fixer runs after the model post-fixer, so we can assume that the second child in view root will always exist.
762
+ view.document.registerPostFixer((writer)=>{
763
+ let hasChanged = false;
764
+ for (const viewRoot of view.document.roots){
765
+ // `viewRoot` can be empty despite the model post-fixers if the model root was detached.
766
+ if (viewRoot.isEmpty) {
767
+ continue;
768
+ }
769
+ // If `viewRoot` is not empty, then we can expect at least two elements in it.
770
+ const body = viewRoot.getChild(1);
771
+ const oldBody = bodyViewElements.get(viewRoot.rootName);
772
+ // If body element has changed we need to disable placeholder on the previous element and enable on the new one.
773
+ if (body !== oldBody) {
774
+ if (oldBody) {
775
+ hidePlaceholder(writer, oldBody);
776
+ writer.removeAttribute('data-placeholder', oldBody);
777
+ }
778
+ writer.setAttribute('data-placeholder', bodyPlaceholder, body);
779
+ bodyViewElements.set(viewRoot.rootName, body);
780
+ hasChanged = true;
781
+ }
782
+ // Then we need to display placeholder if it is needed.
783
+ // See: https://github.com/ckeditor/ckeditor5/issues/8689.
784
+ if (needsPlaceholder(body, true) && viewRoot.childCount === 2 && body.name === 'p') {
785
+ hasChanged = showPlaceholder(writer, body) ? true : hasChanged;
786
+ } else {
787
+ // Or hide if it is not needed.
788
+ hasChanged = hidePlaceholder(writer, body) ? true : hasChanged;
789
+ }
790
+ }
791
+ return hasChanged;
792
+ });
793
+ }
794
+ /**
795
+ * Creates navigation between the title and body sections using <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> keys.
796
+ */ _attachTabPressHandling() {
797
+ const editor = this.editor;
798
+ const model = editor.model;
799
+ // Pressing <kbd>Tab</kbd> inside the title should move the caret to the body.
800
+ editor.keystrokes.set('TAB', (data, cancel)=>{
801
+ model.change((writer)=>{
802
+ const selection = model.document.selection;
803
+ const selectedElements = Array.from(selection.getSelectedBlocks());
804
+ if (selectedElements.length === 1 && selectedElements[0].is('element', 'title-content')) {
805
+ const root = selection.getFirstPosition().root;
806
+ const firstBodyElement = root.getChild(1);
807
+ writer.setSelection(firstBodyElement, 0);
808
+ cancel();
809
+ }
810
+ });
811
+ });
812
+ // Pressing <kbd>Shift</kbd>+<kbd>Tab</kbd> at the beginning of the body should move the caret to the title.
813
+ editor.keystrokes.set('SHIFT + TAB', (data, cancel)=>{
814
+ model.change((writer)=>{
815
+ const selection = model.document.selection;
816
+ if (!selection.isCollapsed) {
817
+ return;
818
+ }
819
+ const selectedElement = first(selection.getSelectedBlocks());
820
+ const selectionPosition = selection.getFirstPosition();
821
+ const root = editor.model.document.getRoot(selectionPosition.root.rootName);
822
+ const title = root.getChild(0);
823
+ const body = root.getChild(1);
824
+ if (selectedElement === body && selectionPosition.isAtStart) {
825
+ writer.setSelection(title.getChild(0), 0);
826
+ cancel();
827
+ }
828
+ });
829
+ });
830
+ }
831
+ }
832
+ /**
833
+ * A view-to-model converter for the h1 that appears at the beginning of the document (a title element).
834
+ *
835
+ * @see module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
836
+ * @param evt An object containing information about the fired event.
837
+ * @param data An object containing conversion input, a placeholder for conversion output and possibly other values.
838
+ * @param conversionApi Conversion interface to be used by the callback.
839
+ */ function dataViewModelH1Insertion(evt, data, conversionApi) {
840
+ const modelCursor = data.modelCursor;
841
+ const viewItem = data.viewItem;
842
+ if (!modelCursor.isAtStart || !modelCursor.parent.is('element', '$root')) {
843
+ return;
844
+ }
845
+ if (!conversionApi.consumable.consume(viewItem, {
846
+ name: true
847
+ })) {
848
+ return;
849
+ }
850
+ const modelWriter = conversionApi.writer;
851
+ const title = modelWriter.createElement('title');
852
+ const titleContent = modelWriter.createElement('title-content');
853
+ modelWriter.append(titleContent, title);
854
+ modelWriter.insert(title, modelCursor);
855
+ conversionApi.convertChildren(viewItem, titleContent);
856
+ conversionApi.updateConversionResult(title, data);
857
+ }
858
+ /**
859
+ * Maps position from the beginning of the model `title` element to the beginning of the view `h1` element.
860
+ *
861
+ * ```html
862
+ * <title>^<title-content>Foo</title-content></title> -> <h1>^Foo</h1>
863
+ * ```
864
+ */ function mapModelPositionToView(editingView) {
865
+ return (evt, data)=>{
866
+ const positionParent = data.modelPosition.parent;
867
+ if (!positionParent.is('element', 'title')) {
868
+ return;
869
+ }
870
+ const modelTitleElement = positionParent.parent;
871
+ const viewElement = data.mapper.toViewElement(modelTitleElement);
872
+ data.viewPosition = editingView.createPositionAt(viewElement, 0);
873
+ evt.stop();
874
+ };
875
+ }
876
+ /**
877
+ * @returns Returns true when given element is a title. Returns false otherwise.
878
+ */ function isTitle(element) {
879
+ return element.is('element', 'title');
880
+ }
881
+ /**
882
+ * Changes the given element to the title element.
883
+ */ function changeElementToTitle(element, writer, model) {
884
+ const title = writer.createElement('title');
885
+ writer.insert(title, element, 'before');
886
+ writer.insert(element, title, 0);
887
+ writer.rename(element, 'title-content');
888
+ model.schema.removeDisallowedAttributes([
889
+ element
890
+ ], writer);
891
+ }
892
+ /**
893
+ * Loops over the list of title elements and fixes additional ones.
894
+ *
895
+ * @returns Returns true when there was any change. Returns false otherwise.
896
+ */ function fixAdditionalTitleElements(titleElements, writer, model) {
897
+ let hasChanged = false;
898
+ for (const title of titleElements){
899
+ if (title.index !== 0) {
900
+ fixTitleElement(title, writer, model);
901
+ hasChanged = true;
902
+ }
903
+ }
904
+ return hasChanged;
905
+ }
906
+ /**
907
+ * Changes given title element to a paragraph or removes it when it is empty.
908
+ */ function fixTitleElement(title, writer, model) {
909
+ const child = title.getChild(0);
910
+ // Empty title should be removed.
911
+ // It is created as a result of pasting to the title element.
912
+ if (child.isEmpty) {
913
+ writer.remove(title);
914
+ return;
915
+ }
916
+ writer.move(writer.createRangeOn(child), title, 'before');
917
+ writer.rename(child, 'paragraph');
918
+ writer.remove(title);
919
+ model.schema.removeDisallowedAttributes([
920
+ child
921
+ ], writer);
922
+ }
923
+ /**
924
+ * Returns true when the last paragraph in the document was created only for the placeholder
925
+ * purpose and it's not needed anymore. Returns false otherwise.
926
+ */ function shouldRemoveLastParagraph(placeholder, root) {
927
+ if (!placeholder || !placeholder.is('element', 'paragraph') || placeholder.childCount) {
928
+ return false;
929
+ }
930
+ if (root.childCount <= 2 || root.getChild(root.childCount - 1) !== placeholder) {
931
+ return false;
932
+ }
933
+ return true;
934
+ }
935
+
936
+ export { Heading, HeadingButtonsUI, HeadingEditing, HeadingUI, Title };
937
+ //# sourceMappingURL=index.js.map