1cceade07176aedada6c04f15bcd27021c558d8f9aa23923d82d3a8b09e3f8b22a35dfc4c3ab96d5005d6048f6463c507e5013ae1f70d11ddb78035df55578 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. var Scope = /* @__PURE__ */ ((Scope2) => (Scope2[Scope2.TYPE = 3] = "TYPE", Scope2[Scope2.LEVEL = 12] = "LEVEL", Scope2[Scope2.ATTRIBUTE = 13] = "ATTRIBUTE", Scope2[Scope2.BLOT = 14] = "BLOT", Scope2[Scope2.INLINE = 7] = "INLINE", Scope2[Scope2.BLOCK = 11] = "BLOCK", Scope2[Scope2.BLOCK_BLOT = 10] = "BLOCK_BLOT", Scope2[Scope2.INLINE_BLOT = 6] = "INLINE_BLOT", Scope2[Scope2.BLOCK_ATTRIBUTE = 9] = "BLOCK_ATTRIBUTE", Scope2[Scope2.INLINE_ATTRIBUTE = 5] = "INLINE_ATTRIBUTE", Scope2[Scope2.ANY = 15] = "ANY", Scope2))(Scope || {});
  2. class Attributor {
  3. constructor(attrName, keyName, options = {}) {
  4. this.attrName = attrName, this.keyName = keyName;
  5. const attributeBit = Scope.TYPE & Scope.ATTRIBUTE;
  6. this.scope = options.scope != null ? (
  7. // Ignore type bits, force attribute bit
  8. options.scope & Scope.LEVEL | attributeBit
  9. ) : Scope.ATTRIBUTE, options.whitelist != null && (this.whitelist = options.whitelist);
  10. }
  11. static keys(node) {
  12. return Array.from(node.attributes).map((item) => item.name);
  13. }
  14. add(node, value) {
  15. return this.canAdd(node, value) ? (node.setAttribute(this.keyName, value), !0) : !1;
  16. }
  17. canAdd(_node, value) {
  18. return this.whitelist == null ? !0 : typeof value == "string" ? this.whitelist.indexOf(value.replace(/["']/g, "")) > -1 : this.whitelist.indexOf(value) > -1;
  19. }
  20. remove(node) {
  21. node.removeAttribute(this.keyName);
  22. }
  23. value(node) {
  24. const value = node.getAttribute(this.keyName);
  25. return this.canAdd(node, value) && value ? value : "";
  26. }
  27. }
  28. class ParchmentError extends Error {
  29. constructor(message) {
  30. message = "[Parchment] " + message, super(message), this.message = message, this.name = this.constructor.name;
  31. }
  32. }
  33. const _Registry = class _Registry {
  34. constructor() {
  35. this.attributes = {}, this.classes = {}, this.tags = {}, this.types = {};
  36. }
  37. static find(node, bubble = !1) {
  38. if (node == null)
  39. return null;
  40. if (this.blots.has(node))
  41. return this.blots.get(node) || null;
  42. if (bubble) {
  43. let parentNode = null;
  44. try {
  45. parentNode = node.parentNode;
  46. } catch {
  47. return null;
  48. }
  49. return this.find(parentNode, bubble);
  50. }
  51. return null;
  52. }
  53. create(scroll, input, value) {
  54. const match2 = this.query(input);
  55. if (match2 == null)
  56. throw new ParchmentError(`Unable to create ${input} blot`);
  57. const blotClass = match2, node = (
  58. // @ts-expect-error Fix me later
  59. input instanceof Node || input.nodeType === Node.TEXT_NODE ? input : blotClass.create(value)
  60. ), blot = new blotClass(scroll, node, value);
  61. return _Registry.blots.set(blot.domNode, blot), blot;
  62. }
  63. find(node, bubble = !1) {
  64. return _Registry.find(node, bubble);
  65. }
  66. query(query, scope = Scope.ANY) {
  67. let match2;
  68. return typeof query == "string" ? match2 = this.types[query] || this.attributes[query] : query instanceof Text || query.nodeType === Node.TEXT_NODE ? match2 = this.types.text : typeof query == "number" ? query & Scope.LEVEL & Scope.BLOCK ? match2 = this.types.block : query & Scope.LEVEL & Scope.INLINE && (match2 = this.types.inline) : query instanceof Element && ((query.getAttribute("class") || "").split(/\s+/).some((name) => (match2 = this.classes[name], !!match2)), match2 = match2 || this.tags[query.tagName]), match2 == null ? null : "scope" in match2 && scope & Scope.LEVEL & match2.scope && scope & Scope.TYPE & match2.scope ? match2 : null;
  69. }
  70. register(...definitions) {
  71. return definitions.map((definition) => {
  72. const isBlot = "blotName" in definition, isAttr = "attrName" in definition;
  73. if (!isBlot && !isAttr)
  74. throw new ParchmentError("Invalid definition");
  75. if (isBlot && definition.blotName === "abstract")
  76. throw new ParchmentError("Cannot register abstract class");
  77. const key = isBlot ? definition.blotName : isAttr ? definition.attrName : void 0;
  78. return this.types[key] = definition, isAttr ? typeof definition.keyName == "string" && (this.attributes[definition.keyName] = definition) : isBlot && (definition.className && (this.classes[definition.className] = definition), definition.tagName && (Array.isArray(definition.tagName) ? definition.tagName = definition.tagName.map((tagName) => tagName.toUpperCase()) : definition.tagName = definition.tagName.toUpperCase(), (Array.isArray(definition.tagName) ? definition.tagName : [definition.tagName]).forEach((tag) => {
  79. (this.tags[tag] == null || definition.className == null) && (this.tags[tag] = definition);
  80. }))), definition;
  81. });
  82. }
  83. };
  84. _Registry.blots = /* @__PURE__ */ new WeakMap();
  85. let Registry = _Registry;
  86. function match(node, prefix) {
  87. return (node.getAttribute("class") || "").split(/\s+/).filter((name) => name.indexOf(`${prefix}-`) === 0);
  88. }
  89. class ClassAttributor extends Attributor {
  90. static keys(node) {
  91. return (node.getAttribute("class") || "").split(/\s+/).map((name) => name.split("-").slice(0, -1).join("-"));
  92. }
  93. add(node, value) {
  94. return this.canAdd(node, value) ? (this.remove(node), node.classList.add(`${this.keyName}-${value}`), !0) : !1;
  95. }
  96. remove(node) {
  97. match(node, this.keyName).forEach((name) => {
  98. node.classList.remove(name);
  99. }), node.classList.length === 0 && node.removeAttribute("class");
  100. }
  101. value(node) {
  102. const value = (match(node, this.keyName)[0] || "").slice(this.keyName.length + 1);
  103. return this.canAdd(node, value) ? value : "";
  104. }
  105. }
  106. const ClassAttributor$1 = ClassAttributor;
  107. function camelize(name) {
  108. const parts = name.split("-"), rest = parts.slice(1).map((part) => part[0].toUpperCase() + part.slice(1)).join("");
  109. return parts[0] + rest;
  110. }
  111. class StyleAttributor extends Attributor {
  112. static keys(node) {
  113. return (node.getAttribute("style") || "").split(";").map((value) => value.split(":")[0].trim());
  114. }
  115. add(node, value) {
  116. return this.canAdd(node, value) ? (node.style[camelize(this.keyName)] = value, !0) : !1;
  117. }
  118. remove(node) {
  119. node.style[camelize(this.keyName)] = "", node.getAttribute("style") || node.removeAttribute("style");
  120. }
  121. value(node) {
  122. const value = node.style[camelize(this.keyName)];
  123. return this.canAdd(node, value) ? value : "";
  124. }
  125. }
  126. const StyleAttributor$1 = StyleAttributor;
  127. class AttributorStore {
  128. constructor(domNode) {
  129. this.attributes = {}, this.domNode = domNode, this.build();
  130. }
  131. attribute(attribute, value) {
  132. value ? attribute.add(this.domNode, value) && (attribute.value(this.domNode) != null ? this.attributes[attribute.attrName] = attribute : delete this.attributes[attribute.attrName]) : (attribute.remove(this.domNode), delete this.attributes[attribute.attrName]);
  133. }
  134. build() {
  135. this.attributes = {};
  136. const blot = Registry.find(this.domNode);
  137. if (blot == null)
  138. return;
  139. const attributes = Attributor.keys(this.domNode), classes = ClassAttributor$1.keys(this.domNode), styles = StyleAttributor$1.keys(this.domNode);
  140. attributes.concat(classes).concat(styles).forEach((name) => {
  141. const attr = blot.scroll.query(name, Scope.ATTRIBUTE);
  142. attr instanceof Attributor && (this.attributes[attr.attrName] = attr);
  143. });
  144. }
  145. copy(target) {
  146. Object.keys(this.attributes).forEach((key) => {
  147. const value = this.attributes[key].value(this.domNode);
  148. target.format(key, value);
  149. });
  150. }
  151. move(target) {
  152. this.copy(target), Object.keys(this.attributes).forEach((key) => {
  153. this.attributes[key].remove(this.domNode);
  154. }), this.attributes = {};
  155. }
  156. values() {
  157. return Object.keys(this.attributes).reduce(
  158. (attributes, name) => (attributes[name] = this.attributes[name].value(this.domNode), attributes),
  159. {}
  160. );
  161. }
  162. }
  163. const AttributorStore$1 = AttributorStore, _ShadowBlot = class _ShadowBlot {
  164. constructor(scroll, domNode) {
  165. this.scroll = scroll, this.domNode = domNode, Registry.blots.set(domNode, this), this.prev = null, this.next = null;
  166. }
  167. static create(rawValue) {
  168. if (this.tagName == null)
  169. throw new ParchmentError("Blot definition missing tagName");
  170. let node, value;
  171. return Array.isArray(this.tagName) ? (typeof rawValue == "string" ? (value = rawValue.toUpperCase(), parseInt(value, 10).toString() === value && (value = parseInt(value, 10))) : typeof rawValue == "number" && (value = rawValue), typeof value == "number" ? node = document.createElement(this.tagName[value - 1]) : value && this.tagName.indexOf(value) > -1 ? node = document.createElement(value) : node = document.createElement(this.tagName[0])) : node = document.createElement(this.tagName), this.className && node.classList.add(this.className), node;
  172. }
  173. // Hack for accessing inherited static methods
  174. get statics() {
  175. return this.constructor;
  176. }
  177. attach() {
  178. }
  179. clone() {
  180. const domNode = this.domNode.cloneNode(!1);
  181. return this.scroll.create(domNode);
  182. }
  183. detach() {
  184. this.parent != null && this.parent.removeChild(this), Registry.blots.delete(this.domNode);
  185. }
  186. deleteAt(index, length) {
  187. this.isolate(index, length).remove();
  188. }
  189. formatAt(index, length, name, value) {
  190. const blot = this.isolate(index, length);
  191. if (this.scroll.query(name, Scope.BLOT) != null && value)
  192. blot.wrap(name, value);
  193. else if (this.scroll.query(name, Scope.ATTRIBUTE) != null) {
  194. const parent = this.scroll.create(this.statics.scope);
  195. blot.wrap(parent), parent.format(name, value);
  196. }
  197. }
  198. insertAt(index, value, def) {
  199. const blot = def == null ? this.scroll.create("text", value) : this.scroll.create(value, def), ref = this.split(index);
  200. this.parent.insertBefore(blot, ref || void 0);
  201. }
  202. isolate(index, length) {
  203. const target = this.split(index);
  204. if (target == null)
  205. throw new Error("Attempt to isolate at end");
  206. return target.split(length), target;
  207. }
  208. length() {
  209. return 1;
  210. }
  211. offset(root = this.parent) {
  212. return this.parent == null || this === root ? 0 : this.parent.children.offset(this) + this.parent.offset(root);
  213. }
  214. optimize(_context) {
  215. this.statics.requiredContainer && !(this.parent instanceof this.statics.requiredContainer) && this.wrap(this.statics.requiredContainer.blotName);
  216. }
  217. remove() {
  218. this.domNode.parentNode != null && this.domNode.parentNode.removeChild(this.domNode), this.detach();
  219. }
  220. replaceWith(name, value) {
  221. const replacement = typeof name == "string" ? this.scroll.create(name, value) : name;
  222. return this.parent != null && (this.parent.insertBefore(replacement, this.next || void 0), this.remove()), replacement;
  223. }
  224. split(index, _force) {
  225. return index === 0 ? this : this.next;
  226. }
  227. update(_mutations, _context) {
  228. }
  229. wrap(name, value) {
  230. const wrapper = typeof name == "string" ? this.scroll.create(name, value) : name;
  231. if (this.parent != null && this.parent.insertBefore(wrapper, this.next || void 0), typeof wrapper.appendChild != "function")
  232. throw new ParchmentError(`Cannot wrap ${name}`);
  233. return wrapper.appendChild(this), wrapper;
  234. }
  235. };
  236. _ShadowBlot.blotName = "abstract";
  237. let ShadowBlot = _ShadowBlot;
  238. const _LeafBlot = class _LeafBlot extends ShadowBlot {
  239. /**
  240. * Returns the value represented by domNode if it is this Blot's type
  241. * No checking that domNode can represent this Blot type is required so
  242. * applications needing it should check externally before calling.
  243. */
  244. static value(_domNode) {
  245. return !0;
  246. }
  247. /**
  248. * Given location represented by node and offset from DOM Selection Range,
  249. * return index to that location.
  250. */
  251. index(node, offset) {
  252. return this.domNode === node || this.domNode.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY ? Math.min(offset, 1) : -1;
  253. }
  254. /**
  255. * Given index to location within blot, return node and offset representing
  256. * that location, consumable by DOM Selection Range
  257. */
  258. position(index, _inclusive) {
  259. let offset = Array.from(this.parent.domNode.childNodes).indexOf(this.domNode);
  260. return index > 0 && (offset += 1), [this.parent.domNode, offset];
  261. }
  262. /**
  263. * Return value represented by this blot
  264. * Should not change without interaction from API or
  265. * user change detectable by update()
  266. */
  267. value() {
  268. return {
  269. [this.statics.blotName]: this.statics.value(this.domNode) || !0
  270. };
  271. }
  272. };
  273. _LeafBlot.scope = Scope.INLINE_BLOT;
  274. let LeafBlot = _LeafBlot;
  275. const LeafBlot$1 = LeafBlot;
  276. class LinkedList {
  277. constructor() {
  278. this.head = null, this.tail = null, this.length = 0;
  279. }
  280. append(...nodes) {
  281. if (this.insertBefore(nodes[0], null), nodes.length > 1) {
  282. const rest = nodes.slice(1);
  283. this.append(...rest);
  284. }
  285. }
  286. at(index) {
  287. const next = this.iterator();
  288. let cur = next();
  289. for (; cur && index > 0; )
  290. index -= 1, cur = next();
  291. return cur;
  292. }
  293. contains(node) {
  294. const next = this.iterator();
  295. let cur = next();
  296. for (; cur; ) {
  297. if (cur === node)
  298. return !0;
  299. cur = next();
  300. }
  301. return !1;
  302. }
  303. indexOf(node) {
  304. const next = this.iterator();
  305. let cur = next(), index = 0;
  306. for (; cur; ) {
  307. if (cur === node)
  308. return index;
  309. index += 1, cur = next();
  310. }
  311. return -1;
  312. }
  313. insertBefore(node, refNode) {
  314. node != null && (this.remove(node), node.next = refNode, refNode != null ? (node.prev = refNode.prev, refNode.prev != null && (refNode.prev.next = node), refNode.prev = node, refNode === this.head && (this.head = node)) : this.tail != null ? (this.tail.next = node, node.prev = this.tail, this.tail = node) : (node.prev = null, this.head = this.tail = node), this.length += 1);
  315. }
  316. offset(target) {
  317. let index = 0, cur = this.head;
  318. for (; cur != null; ) {
  319. if (cur === target)
  320. return index;
  321. index += cur.length(), cur = cur.next;
  322. }
  323. return -1;
  324. }
  325. remove(node) {
  326. this.contains(node) && (node.prev != null && (node.prev.next = node.next), node.next != null && (node.next.prev = node.prev), node === this.head && (this.head = node.next), node === this.tail && (this.tail = node.prev), this.length -= 1);
  327. }
  328. iterator(curNode = this.head) {
  329. return () => {
  330. const ret = curNode;
  331. return curNode != null && (curNode = curNode.next), ret;
  332. };
  333. }
  334. find(index, inclusive = !1) {
  335. const next = this.iterator();
  336. let cur = next();
  337. for (; cur; ) {
  338. const length = cur.length();
  339. if (index < length || inclusive && index === length && (cur.next == null || cur.next.length() !== 0))
  340. return [cur, index];
  341. index -= length, cur = next();
  342. }
  343. return [null, 0];
  344. }
  345. forEach(callback) {
  346. const next = this.iterator();
  347. let cur = next();
  348. for (; cur; )
  349. callback(cur), cur = next();
  350. }
  351. forEachAt(index, length, callback) {
  352. if (length <= 0)
  353. return;
  354. const [startNode, offset] = this.find(index);
  355. let curIndex = index - offset;
  356. const next = this.iterator(startNode);
  357. let cur = next();
  358. for (; cur && curIndex < index + length; ) {
  359. const curLength = cur.length();
  360. index > curIndex ? callback(
  361. cur,
  362. index - curIndex,
  363. Math.min(length, curIndex + curLength - index)
  364. ) : callback(cur, 0, Math.min(curLength, index + length - curIndex)), curIndex += curLength, cur = next();
  365. }
  366. }
  367. map(callback) {
  368. return this.reduce((memo, cur) => (memo.push(callback(cur)), memo), []);
  369. }
  370. reduce(callback, memo) {
  371. const next = this.iterator();
  372. let cur = next();
  373. for (; cur; )
  374. memo = callback(memo, cur), cur = next();
  375. return memo;
  376. }
  377. }
  378. function makeAttachedBlot(node, scroll) {
  379. const found = scroll.find(node);
  380. if (found)
  381. return found;
  382. try {
  383. return scroll.create(node);
  384. } catch {
  385. const blot = scroll.create(Scope.INLINE);
  386. return Array.from(node.childNodes).forEach((child) => {
  387. blot.domNode.appendChild(child);
  388. }), node.parentNode && node.parentNode.replaceChild(blot.domNode, node), blot.attach(), blot;
  389. }
  390. }
  391. const _ParentBlot = class _ParentBlot extends ShadowBlot {
  392. constructor(scroll, domNode) {
  393. super(scroll, domNode), this.uiNode = null, this.build();
  394. }
  395. appendChild(other) {
  396. this.insertBefore(other);
  397. }
  398. attach() {
  399. super.attach(), this.children.forEach((child) => {
  400. child.attach();
  401. });
  402. }
  403. attachUI(node) {
  404. this.uiNode != null && this.uiNode.remove(), this.uiNode = node, _ParentBlot.uiClass && this.uiNode.classList.add(_ParentBlot.uiClass), this.uiNode.setAttribute("contenteditable", "false"), this.domNode.insertBefore(this.uiNode, this.domNode.firstChild);
  405. }
  406. /**
  407. * Called during construction, should fill its own children LinkedList.
  408. */
  409. build() {
  410. this.children = new LinkedList(), Array.from(this.domNode.childNodes).filter((node) => node !== this.uiNode).reverse().forEach((node) => {
  411. try {
  412. const child = makeAttachedBlot(node, this.scroll);
  413. this.insertBefore(child, this.children.head || void 0);
  414. } catch (err) {
  415. if (err instanceof ParchmentError)
  416. return;
  417. throw err;
  418. }
  419. });
  420. }
  421. deleteAt(index, length) {
  422. if (index === 0 && length === this.length())
  423. return this.remove();
  424. this.children.forEachAt(index, length, (child, offset, childLength) => {
  425. child.deleteAt(offset, childLength);
  426. });
  427. }
  428. descendant(criteria, index = 0) {
  429. const [child, offset] = this.children.find(index);
  430. return criteria.blotName == null && criteria(child) || criteria.blotName != null && child instanceof criteria ? [child, offset] : child instanceof _ParentBlot ? child.descendant(criteria, offset) : [null, -1];
  431. }
  432. descendants(criteria, index = 0, length = Number.MAX_VALUE) {
  433. let descendants = [], lengthLeft = length;
  434. return this.children.forEachAt(
  435. index,
  436. length,
  437. (child, childIndex, childLength) => {
  438. (criteria.blotName == null && criteria(child) || criteria.blotName != null && child instanceof criteria) && descendants.push(child), child instanceof _ParentBlot && (descendants = descendants.concat(
  439. child.descendants(criteria, childIndex, lengthLeft)
  440. )), lengthLeft -= childLength;
  441. }
  442. ), descendants;
  443. }
  444. detach() {
  445. this.children.forEach((child) => {
  446. child.detach();
  447. }), super.detach();
  448. }
  449. enforceAllowedChildren() {
  450. let done = !1;
  451. this.children.forEach((child) => {
  452. done || this.statics.allowedChildren.some(
  453. (def) => child instanceof def
  454. ) || (child.statics.scope === Scope.BLOCK_BLOT ? (child.next != null && this.splitAfter(child), child.prev != null && this.splitAfter(child.prev), child.parent.unwrap(), done = !0) : child instanceof _ParentBlot ? child.unwrap() : child.remove());
  455. });
  456. }
  457. formatAt(index, length, name, value) {
  458. this.children.forEachAt(index, length, (child, offset, childLength) => {
  459. child.formatAt(offset, childLength, name, value);
  460. });
  461. }
  462. insertAt(index, value, def) {
  463. const [child, offset] = this.children.find(index);
  464. if (child)
  465. child.insertAt(offset, value, def);
  466. else {
  467. const blot = def == null ? this.scroll.create("text", value) : this.scroll.create(value, def);
  468. this.appendChild(blot);
  469. }
  470. }
  471. insertBefore(childBlot, refBlot) {
  472. childBlot.parent != null && childBlot.parent.children.remove(childBlot);
  473. let refDomNode = null;
  474. this.children.insertBefore(childBlot, refBlot || null), childBlot.parent = this, refBlot != null && (refDomNode = refBlot.domNode), (this.domNode.parentNode !== childBlot.domNode || this.domNode.nextSibling !== refDomNode) && this.domNode.insertBefore(childBlot.domNode, refDomNode), childBlot.attach();
  475. }
  476. length() {
  477. return this.children.reduce((memo, child) => memo + child.length(), 0);
  478. }
  479. moveChildren(targetParent, refNode) {
  480. this.children.forEach((child) => {
  481. targetParent.insertBefore(child, refNode);
  482. });
  483. }
  484. optimize(context) {
  485. if (super.optimize(context), this.enforceAllowedChildren(), this.uiNode != null && this.uiNode !== this.domNode.firstChild && this.domNode.insertBefore(this.uiNode, this.domNode.firstChild), this.children.length === 0)
  486. if (this.statics.defaultChild != null) {
  487. const child = this.scroll.create(this.statics.defaultChild.blotName);
  488. this.appendChild(child);
  489. } else
  490. this.remove();
  491. }
  492. path(index, inclusive = !1) {
  493. const [child, offset] = this.children.find(index, inclusive), position = [[this, index]];
  494. return child instanceof _ParentBlot ? position.concat(child.path(offset, inclusive)) : (child != null && position.push([child, offset]), position);
  495. }
  496. removeChild(child) {
  497. this.children.remove(child);
  498. }
  499. replaceWith(name, value) {
  500. const replacement = typeof name == "string" ? this.scroll.create(name, value) : name;
  501. return replacement instanceof _ParentBlot && this.moveChildren(replacement), super.replaceWith(replacement);
  502. }
  503. split(index, force = !1) {
  504. if (!force) {
  505. if (index === 0)
  506. return this;
  507. if (index === this.length())
  508. return this.next;
  509. }
  510. const after = this.clone();
  511. return this.parent && this.parent.insertBefore(after, this.next || void 0), this.children.forEachAt(index, this.length(), (child, offset, _length) => {
  512. const split = child.split(offset, force);
  513. split != null && after.appendChild(split);
  514. }), after;
  515. }
  516. splitAfter(child) {
  517. const after = this.clone();
  518. for (; child.next != null; )
  519. after.appendChild(child.next);
  520. return this.parent && this.parent.insertBefore(after, this.next || void 0), after;
  521. }
  522. unwrap() {
  523. this.parent && this.moveChildren(this.parent, this.next || void 0), this.remove();
  524. }
  525. update(mutations, _context) {
  526. const addedNodes = [], removedNodes = [];
  527. mutations.forEach((mutation) => {
  528. mutation.target === this.domNode && mutation.type === "childList" && (addedNodes.push(...mutation.addedNodes), removedNodes.push(...mutation.removedNodes));
  529. }), removedNodes.forEach((node) => {
  530. if (node.parentNode != null && // @ts-expect-error Fix me later
  531. node.tagName !== "IFRAME" && document.body.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY)
  532. return;
  533. const blot = this.scroll.find(node);
  534. blot != null && (blot.domNode.parentNode == null || blot.domNode.parentNode === this.domNode) && blot.detach();
  535. }), addedNodes.filter((node) => node.parentNode === this.domNode && node !== this.uiNode).sort((a, b) => a === b ? 0 : a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? 1 : -1).forEach((node) => {
  536. let refBlot = null;
  537. node.nextSibling != null && (refBlot = this.scroll.find(node.nextSibling));
  538. const blot = makeAttachedBlot(node, this.scroll);
  539. (blot.next !== refBlot || blot.next == null) && (blot.parent != null && blot.parent.removeChild(this), this.insertBefore(blot, refBlot || void 0));
  540. }), this.enforceAllowedChildren();
  541. }
  542. };
  543. _ParentBlot.uiClass = "";
  544. let ParentBlot = _ParentBlot;
  545. const ParentBlot$1 = ParentBlot;
  546. function isEqual(obj1, obj2) {
  547. if (Object.keys(obj1).length !== Object.keys(obj2).length)
  548. return !1;
  549. for (const prop in obj1)
  550. if (obj1[prop] !== obj2[prop])
  551. return !1;
  552. return !0;
  553. }
  554. const _InlineBlot = class _InlineBlot extends ParentBlot$1 {
  555. static create(value) {
  556. return super.create(value);
  557. }
  558. static formats(domNode, scroll) {
  559. const match2 = scroll.query(_InlineBlot.blotName);
  560. if (!(match2 != null && domNode.tagName === match2.tagName)) {
  561. if (typeof this.tagName == "string")
  562. return !0;
  563. if (Array.isArray(this.tagName))
  564. return domNode.tagName.toLowerCase();
  565. }
  566. }
  567. constructor(scroll, domNode) {
  568. super(scroll, domNode), this.attributes = new AttributorStore$1(this.domNode);
  569. }
  570. format(name, value) {
  571. if (name === this.statics.blotName && !value)
  572. this.children.forEach((child) => {
  573. child instanceof _InlineBlot || (child = child.wrap(_InlineBlot.blotName, !0)), this.attributes.copy(child);
  574. }), this.unwrap();
  575. else {
  576. const format = this.scroll.query(name, Scope.INLINE);
  577. if (format == null)
  578. return;
  579. format instanceof Attributor ? this.attributes.attribute(format, value) : value && (name !== this.statics.blotName || this.formats()[name] !== value) && this.replaceWith(name, value);
  580. }
  581. }
  582. formats() {
  583. const formats = this.attributes.values(), format = this.statics.formats(this.domNode, this.scroll);
  584. return format != null && (formats[this.statics.blotName] = format), formats;
  585. }
  586. formatAt(index, length, name, value) {
  587. this.formats()[name] != null || this.scroll.query(name, Scope.ATTRIBUTE) ? this.isolate(index, length).format(name, value) : super.formatAt(index, length, name, value);
  588. }
  589. optimize(context) {
  590. super.optimize(context);
  591. const formats = this.formats();
  592. if (Object.keys(formats).length === 0)
  593. return this.unwrap();
  594. const next = this.next;
  595. next instanceof _InlineBlot && next.prev === this && isEqual(formats, next.formats()) && (next.moveChildren(this), next.remove());
  596. }
  597. replaceWith(name, value) {
  598. const replacement = super.replaceWith(name, value);
  599. return this.attributes.copy(replacement), replacement;
  600. }
  601. update(mutations, context) {
  602. super.update(mutations, context), mutations.some(
  603. (mutation) => mutation.target === this.domNode && mutation.type === "attributes"
  604. ) && this.attributes.build();
  605. }
  606. wrap(name, value) {
  607. const wrapper = super.wrap(name, value);
  608. return wrapper instanceof _InlineBlot && this.attributes.move(wrapper), wrapper;
  609. }
  610. };
  611. _InlineBlot.allowedChildren = [_InlineBlot, LeafBlot$1], _InlineBlot.blotName = "inline", _InlineBlot.scope = Scope.INLINE_BLOT, _InlineBlot.tagName = "SPAN";
  612. let InlineBlot = _InlineBlot;
  613. const InlineBlot$1 = InlineBlot, _BlockBlot = class _BlockBlot extends ParentBlot$1 {
  614. static create(value) {
  615. return super.create(value);
  616. }
  617. static formats(domNode, scroll) {
  618. const match2 = scroll.query(_BlockBlot.blotName);
  619. if (!(match2 != null && domNode.tagName === match2.tagName)) {
  620. if (typeof this.tagName == "string")
  621. return !0;
  622. if (Array.isArray(this.tagName))
  623. return domNode.tagName.toLowerCase();
  624. }
  625. }
  626. constructor(scroll, domNode) {
  627. super(scroll, domNode), this.attributes = new AttributorStore$1(this.domNode);
  628. }
  629. format(name, value) {
  630. const format = this.scroll.query(name, Scope.BLOCK);
  631. format != null && (format instanceof Attributor ? this.attributes.attribute(format, value) : name === this.statics.blotName && !value ? this.replaceWith(_BlockBlot.blotName) : value && (name !== this.statics.blotName || this.formats()[name] !== value) && this.replaceWith(name, value));
  632. }
  633. formats() {
  634. const formats = this.attributes.values(), format = this.statics.formats(this.domNode, this.scroll);
  635. return format != null && (formats[this.statics.blotName] = format), formats;
  636. }
  637. formatAt(index, length, name, value) {
  638. this.scroll.query(name, Scope.BLOCK) != null ? this.format(name, value) : super.formatAt(index, length, name, value);
  639. }
  640. insertAt(index, value, def) {
  641. if (def == null || this.scroll.query(value, Scope.INLINE) != null)
  642. super.insertAt(index, value, def);
  643. else {
  644. const after = this.split(index);
  645. if (after != null) {
  646. const blot = this.scroll.create(value, def);
  647. after.parent.insertBefore(blot, after);
  648. } else
  649. throw new Error("Attempt to insertAt after block boundaries");
  650. }
  651. }
  652. replaceWith(name, value) {
  653. const replacement = super.replaceWith(name, value);
  654. return this.attributes.copy(replacement), replacement;
  655. }
  656. update(mutations, context) {
  657. super.update(mutations, context), mutations.some(
  658. (mutation) => mutation.target === this.domNode && mutation.type === "attributes"
  659. ) && this.attributes.build();
  660. }
  661. };
  662. _BlockBlot.blotName = "block", _BlockBlot.scope = Scope.BLOCK_BLOT, _BlockBlot.tagName = "P", _BlockBlot.allowedChildren = [
  663. InlineBlot$1,
  664. _BlockBlot,
  665. LeafBlot$1
  666. ];
  667. let BlockBlot = _BlockBlot;
  668. const BlockBlot$1 = BlockBlot, _ContainerBlot = class _ContainerBlot extends ParentBlot$1 {
  669. checkMerge() {
  670. return this.next !== null && this.next.statics.blotName === this.statics.blotName;
  671. }
  672. deleteAt(index, length) {
  673. super.deleteAt(index, length), this.enforceAllowedChildren();
  674. }
  675. formatAt(index, length, name, value) {
  676. super.formatAt(index, length, name, value), this.enforceAllowedChildren();
  677. }
  678. insertAt(index, value, def) {
  679. super.insertAt(index, value, def), this.enforceAllowedChildren();
  680. }
  681. optimize(context) {
  682. super.optimize(context), this.children.length > 0 && this.next != null && this.checkMerge() && (this.next.moveChildren(this), this.next.remove());
  683. }
  684. };
  685. _ContainerBlot.blotName = "container", _ContainerBlot.scope = Scope.BLOCK_BLOT;
  686. let ContainerBlot = _ContainerBlot;
  687. const ContainerBlot$1 = ContainerBlot;
  688. class EmbedBlot extends LeafBlot$1 {
  689. static formats(_domNode, _scroll) {
  690. }
  691. format(name, value) {
  692. super.formatAt(0, this.length(), name, value);
  693. }
  694. formatAt(index, length, name, value) {
  695. index === 0 && length === this.length() ? this.format(name, value) : super.formatAt(index, length, name, value);
  696. }
  697. formats() {
  698. return this.statics.formats(this.domNode, this.scroll);
  699. }
  700. }
  701. const EmbedBlot$1 = EmbedBlot, OBSERVER_CONFIG = {
  702. attributes: !0,
  703. characterData: !0,
  704. characterDataOldValue: !0,
  705. childList: !0,
  706. subtree: !0
  707. }, MAX_OPTIMIZE_ITERATIONS = 100, _ScrollBlot = class _ScrollBlot extends ParentBlot$1 {
  708. constructor(registry, node) {
  709. super(null, node), this.registry = registry, this.scroll = this, this.build(), this.observer = new MutationObserver((mutations) => {
  710. this.update(mutations);
  711. }), this.observer.observe(this.domNode, OBSERVER_CONFIG), this.attach();
  712. }
  713. create(input, value) {
  714. return this.registry.create(this, input, value);
  715. }
  716. find(node, bubble = !1) {
  717. const blot = this.registry.find(node, bubble);
  718. return blot ? blot.scroll === this ? blot : bubble ? this.find(blot.scroll.domNode.parentNode, !0) : null : null;
  719. }
  720. query(query, scope = Scope.ANY) {
  721. return this.registry.query(query, scope);
  722. }
  723. register(...definitions) {
  724. return this.registry.register(...definitions);
  725. }
  726. build() {
  727. this.scroll != null && super.build();
  728. }
  729. detach() {
  730. super.detach(), this.observer.disconnect();
  731. }
  732. deleteAt(index, length) {
  733. this.update(), index === 0 && length === this.length() ? this.children.forEach((child) => {
  734. child.remove();
  735. }) : super.deleteAt(index, length);
  736. }
  737. formatAt(index, length, name, value) {
  738. this.update(), super.formatAt(index, length, name, value);
  739. }
  740. insertAt(index, value, def) {
  741. this.update(), super.insertAt(index, value, def);
  742. }
  743. optimize(mutations = [], context = {}) {
  744. super.optimize(context);
  745. const mutationsMap = context.mutationsMap || /* @__PURE__ */ new WeakMap();
  746. let records = Array.from(this.observer.takeRecords());
  747. for (; records.length > 0; )
  748. mutations.push(records.pop());
  749. const mark = (blot, markParent = !0) => {
  750. blot == null || blot === this || blot.domNode.parentNode != null && (mutationsMap.has(blot.domNode) || mutationsMap.set(blot.domNode, []), markParent && mark(blot.parent));
  751. }, optimize = (blot) => {
  752. mutationsMap.has(blot.domNode) && (blot instanceof ParentBlot$1 && blot.children.forEach(optimize), mutationsMap.delete(blot.domNode), blot.optimize(context));
  753. };
  754. let remaining = mutations;
  755. for (let i = 0; remaining.length > 0; i += 1) {
  756. if (i >= MAX_OPTIMIZE_ITERATIONS)
  757. throw new Error("[Parchment] Maximum optimize iterations reached");
  758. for (remaining.forEach((mutation) => {
  759. const blot = this.find(mutation.target, !0);
  760. blot != null && (blot.domNode === mutation.target && (mutation.type === "childList" ? (mark(this.find(mutation.previousSibling, !1)), Array.from(mutation.addedNodes).forEach((node) => {
  761. const child = this.find(node, !1);
  762. mark(child, !1), child instanceof ParentBlot$1 && child.children.forEach((grandChild) => {
  763. mark(grandChild, !1);
  764. });
  765. })) : mutation.type === "attributes" && mark(blot.prev)), mark(blot));
  766. }), this.children.forEach(optimize), remaining = Array.from(this.observer.takeRecords()), records = remaining.slice(); records.length > 0; )
  767. mutations.push(records.pop());
  768. }
  769. }
  770. update(mutations, context = {}) {
  771. mutations = mutations || this.observer.takeRecords();
  772. const mutationsMap = /* @__PURE__ */ new WeakMap();
  773. mutations.map((mutation) => {
  774. const blot = this.find(mutation.target, !0);
  775. return blot == null ? null : mutationsMap.has(blot.domNode) ? (mutationsMap.get(blot.domNode).push(mutation), null) : (mutationsMap.set(blot.domNode, [mutation]), blot);
  776. }).forEach((blot) => {
  777. blot != null && blot !== this && mutationsMap.has(blot.domNode) && blot.update(mutationsMap.get(blot.domNode) || [], context);
  778. }), context.mutationsMap = mutationsMap, mutationsMap.has(this.domNode) && super.update(mutationsMap.get(this.domNode), context), this.optimize(mutations, context);
  779. }
  780. };
  781. _ScrollBlot.blotName = "scroll", _ScrollBlot.defaultChild = BlockBlot$1, _ScrollBlot.allowedChildren = [BlockBlot$1, ContainerBlot$1], _ScrollBlot.scope = Scope.BLOCK_BLOT, _ScrollBlot.tagName = "DIV";
  782. let ScrollBlot = _ScrollBlot;
  783. const ScrollBlot$1 = ScrollBlot, _TextBlot = class _TextBlot extends LeafBlot$1 {
  784. static create(value) {
  785. return document.createTextNode(value);
  786. }
  787. static value(domNode) {
  788. return domNode.data;
  789. }
  790. constructor(scroll, node) {
  791. super(scroll, node), this.text = this.statics.value(this.domNode);
  792. }
  793. deleteAt(index, length) {
  794. this.domNode.data = this.text = this.text.slice(0, index) + this.text.slice(index + length);
  795. }
  796. index(node, offset) {
  797. return this.domNode === node ? offset : -1;
  798. }
  799. insertAt(index, value, def) {
  800. def == null ? (this.text = this.text.slice(0, index) + value + this.text.slice(index), this.domNode.data = this.text) : super.insertAt(index, value, def);
  801. }
  802. length() {
  803. return this.text.length;
  804. }
  805. optimize(context) {
  806. super.optimize(context), this.text = this.statics.value(this.domNode), this.text.length === 0 ? this.remove() : this.next instanceof _TextBlot && this.next.prev === this && (this.insertAt(this.length(), this.next.value()), this.next.remove());
  807. }
  808. position(index, _inclusive = !1) {
  809. return [this.domNode, index];
  810. }
  811. split(index, force = !1) {
  812. if (!force) {
  813. if (index === 0)
  814. return this;
  815. if (index === this.length())
  816. return this.next;
  817. }
  818. const after = this.scroll.create(this.domNode.splitText(index));
  819. return this.parent.insertBefore(after, this.next || void 0), this.text = this.statics.value(this.domNode), after;
  820. }
  821. update(mutations, _context) {
  822. mutations.some((mutation) => mutation.type === "characterData" && mutation.target === this.domNode) && (this.text = this.statics.value(this.domNode));
  823. }
  824. value() {
  825. return this.text;
  826. }
  827. };
  828. _TextBlot.blotName = "text", _TextBlot.scope = Scope.INLINE_BLOT;
  829. let TextBlot = _TextBlot;
  830. const TextBlot$1 = TextBlot;
  831. export {
  832. Attributor,
  833. AttributorStore$1 as AttributorStore,
  834. BlockBlot$1 as BlockBlot,
  835. ClassAttributor$1 as ClassAttributor,
  836. ContainerBlot$1 as ContainerBlot,
  837. EmbedBlot$1 as EmbedBlot,
  838. InlineBlot$1 as InlineBlot,
  839. LeafBlot$1 as LeafBlot,
  840. ParentBlot$1 as ParentBlot,
  841. Registry,
  842. Scope,
  843. ScrollBlot$1 as ScrollBlot,
  844. StyleAttributor$1 as StyleAttributor,
  845. TextBlot$1 as TextBlot
  846. };
  847. //# sourceMappingURL=parchment.js.map