991f620d3154256e3a6de4c319016221869e890fc7297ea052fed1b0ebf4a161863e009ba028f0581376c0e687277b2c501e677cf2d3a5bb4b0af388b9ab16 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. export declare class Attributor {
  2. readonly attrName: string;
  3. readonly keyName: string;
  4. static keys(node: HTMLElement): string[];
  5. scope: Scope;
  6. whitelist: string[] | undefined;
  7. constructor(attrName: string, keyName: string, options?: AttributorOptions);
  8. add(node: HTMLElement, value: any): boolean;
  9. canAdd(_node: HTMLElement, value: any): boolean;
  10. remove(node: HTMLElement): void;
  11. value(node: HTMLElement): any;
  12. }
  13. export declare interface AttributorOptions {
  14. scope?: Scope;
  15. whitelist?: string[];
  16. }
  17. export declare class AttributorStore {
  18. private attributes;
  19. private domNode;
  20. constructor(domNode: HTMLElement);
  21. attribute(attribute: Attributor, value: any): void;
  22. build(): void;
  23. copy(target: Formattable): void;
  24. move(target: Formattable): void;
  25. values(): {
  26. [key: string]: any;
  27. };
  28. }
  29. export declare class BlockBlot extends ParentBlot implements Formattable {
  30. static blotName: string;
  31. static scope: Scope;
  32. static tagName: string | string[];
  33. static allowedChildren: BlotConstructor[];
  34. static create(value?: unknown): HTMLElement;
  35. static formats(domNode: HTMLElement, scroll: Root): any;
  36. protected attributes: AttributorStore;
  37. constructor(scroll: Root, domNode: Node);
  38. format(name: string, value: any): void;
  39. formats(): {
  40. [index: string]: any;
  41. };
  42. formatAt(index: number, length: number, name: string, value: any): void;
  43. insertAt(index: number, value: string, def?: any): void;
  44. replaceWith(name: string | Blot, value?: any): Blot;
  45. update(mutations: MutationRecord[], context: {
  46. [key: string]: any;
  47. }): void;
  48. }
  49. /**
  50. * Blots are the basic building blocks of a Parchment document.
  51. *
  52. * Several basic implementations such as Block, Inline, and Embed are provided.
  53. * In general you will want to extend one of these, instead of building from scratch.
  54. * After implementation, blots need to be registered before usage.
  55. *
  56. * At the very minimum a Blot must be named with a static blotName and associated with either a tagName or className.
  57. * If a Blot is defined with both a tag and class, the class takes precedence, but the tag may be used as a fallback.
  58. * Blots must also have a scope, which determine if it is inline or block.
  59. */
  60. export declare interface Blot extends LinkedNode {
  61. scroll: Root;
  62. parent: Parent;
  63. prev: Blot | null;
  64. next: Blot | null;
  65. domNode: Node;
  66. statics: BlotConstructor;
  67. attach(): void;
  68. clone(): Blot;
  69. detach(): void;
  70. isolate(index: number, length: number): Blot;
  71. /**
  72. * For leaves, length of blot's value()
  73. * For parents, sum of children's values
  74. */
  75. length(): number;
  76. /**
  77. * Returns offset between this blot and an ancestor's
  78. */
  79. offset(root?: Blot): number;
  80. remove(): void;
  81. replaceWith(name: string, value: any): Blot;
  82. replaceWith(replacement: Blot): Blot;
  83. split(index: number, force?: boolean): Blot | null;
  84. wrap(name: string, value?: any): Parent;
  85. wrap(wrapper: Parent): Parent;
  86. deleteAt(index: number, length: number): void;
  87. formatAt(index: number, length: number, name: string, value: any): void;
  88. insertAt(index: number, value: string, def?: any): void;
  89. /**
  90. * Called after update cycle completes. Cannot change the value or length
  91. * of the document, and any DOM operation must reduce complexity of the DOM
  92. * tree. A shared context object is passed through all blots.
  93. */
  94. optimize(context: {
  95. [key: string]: any;
  96. }): void;
  97. optimize(mutations: MutationRecord[], context: {
  98. [key: string]: any;
  99. }): void;
  100. /**
  101. * Called when blot changes, with the mutation records of its change.
  102. * Internal records of the blot values can be updated, and modifications of
  103. * the blot itself is permitted. Can be trigger from user change or API call.
  104. * A shared context object is passed through all blots.
  105. */
  106. update(mutations: MutationRecord[], context: {
  107. [key: string]: any;
  108. }): void;
  109. }
  110. export declare interface BlotConstructor {
  111. new (...args: any[]): Blot;
  112. /**
  113. * Creates corresponding DOM node
  114. */
  115. create(value?: any): Node;
  116. blotName: string;
  117. tagName: string | string[];
  118. scope: Scope;
  119. className?: string;
  120. requiredContainer?: BlotConstructor;
  121. allowedChildren?: BlotConstructor[];
  122. defaultChild?: BlotConstructor;
  123. }
  124. export declare class ClassAttributor extends Attributor {
  125. static keys(node: HTMLElement): string[];
  126. add(node: HTMLElement, value: any): boolean;
  127. remove(node: HTMLElement): void;
  128. value(node: HTMLElement): any;
  129. }
  130. export declare class ContainerBlot extends ParentBlot {
  131. static blotName: string;
  132. static scope: Scope;
  133. static tagName: string | string[];
  134. prev: BlockBlot | ContainerBlot | null;
  135. next: BlockBlot | ContainerBlot | null;
  136. checkMerge(): boolean;
  137. deleteAt(index: number, length: number): void;
  138. formatAt(index: number, length: number, name: string, value: any): void;
  139. insertAt(index: number, value: string, def?: any): void;
  140. optimize(context: {
  141. [key: string]: any;
  142. }): void;
  143. }
  144. export declare class EmbedBlot extends LeafBlot implements Formattable {
  145. static formats(_domNode: HTMLElement, _scroll: Root): any;
  146. format(name: string, value: any): void;
  147. formatAt(index: number, length: number, name: string, value: any): void;
  148. formats(): {
  149. [index: string]: any;
  150. };
  151. }
  152. export declare interface Formattable extends Blot {
  153. /**
  154. * Apply format to blot. Should not pass onto child or other blot.
  155. */
  156. format(name: string, value: any): void;
  157. /**
  158. * Return formats represented by blot, including from Attributors.
  159. */
  160. formats(): {
  161. [index: string]: any;
  162. };
  163. }
  164. export declare class InlineBlot extends ParentBlot implements Formattable {
  165. static allowedChildren: BlotConstructor[];
  166. static blotName: string;
  167. static scope: Scope;
  168. static tagName: string | string[];
  169. static create(value?: unknown): HTMLElement;
  170. static formats(domNode: HTMLElement, scroll: Root): any;
  171. protected attributes: AttributorStore;
  172. constructor(scroll: Root, domNode: Node);
  173. format(name: string, value: any): void;
  174. formats(): {
  175. [index: string]: any;
  176. };
  177. formatAt(index: number, length: number, name: string, value: any): void;
  178. optimize(context: {
  179. [key: string]: any;
  180. }): void;
  181. replaceWith(name: string | Blot, value?: any): Blot;
  182. update(mutations: MutationRecord[], context: {
  183. [key: string]: any;
  184. }): void;
  185. wrap(name: string | Parent, value?: any): Parent;
  186. }
  187. export declare interface Leaf extends Blot {
  188. index(node: Node, offset: number): number;
  189. position(index: number, inclusive: boolean): [Node, number];
  190. value(): any;
  191. }
  192. export declare class LeafBlot extends ShadowBlot implements Leaf {
  193. static scope: Scope;
  194. /**
  195. * Returns the value represented by domNode if it is this Blot's type
  196. * No checking that domNode can represent this Blot type is required so
  197. * applications needing it should check externally before calling.
  198. */
  199. static value(_domNode: Node): any;
  200. /**
  201. * Given location represented by node and offset from DOM Selection Range,
  202. * return index to that location.
  203. */
  204. index(node: Node, offset: number): number;
  205. /**
  206. * Given index to location within blot, return node and offset representing
  207. * that location, consumable by DOM Selection Range
  208. */
  209. position(index: number, _inclusive?: boolean): [Node, number];
  210. /**
  211. * Return value represented by this blot
  212. * Should not change without interaction from API or
  213. * user change detectable by update()
  214. */
  215. value(): any;
  216. }
  217. export declare class LinkedList<T extends LinkedNode> {
  218. head: T | null;
  219. tail: T | null;
  220. length: number;
  221. constructor();
  222. append(...nodes: T[]): void;
  223. at(index: number): T | null;
  224. contains(node: T): boolean;
  225. indexOf(node: T): number;
  226. insertBefore(node: T | null, refNode: T | null): void;
  227. offset(target: T): number;
  228. remove(node: T): void;
  229. iterator(curNode?: T | null): () => T | null;
  230. find(index: number, inclusive?: boolean): [T | null, number];
  231. forEach(callback: (cur: T) => void): void;
  232. forEachAt(index: number, length: number, callback: (cur: T, offset: number, length: number) => void): void;
  233. map(callback: (cur: T) => any): any[];
  234. reduce<M>(callback: (memo: M, cur: T) => M, memo: M): M;
  235. }
  236. export declare interface LinkedNode {
  237. prev: LinkedNode | null;
  238. next: LinkedNode | null;
  239. length(): number;
  240. }
  241. export declare interface Parent extends Blot {
  242. children: LinkedList<Blot>;
  243. domNode: HTMLElement;
  244. appendChild(child: Blot): void;
  245. descendant<T>(type: new () => T, index: number): [T, number];
  246. descendant<T>(matcher: (blot: Blot) => boolean, index: number): [T, number];
  247. descendants<T>(type: new () => T, index: number, length: number): T[];
  248. descendants<T>(matcher: (blot: Blot) => boolean, index: number, length: number): T[];
  249. insertBefore(child: Blot, refNode?: Blot | null): void;
  250. moveChildren(parent: Parent, refNode?: Blot | null): void;
  251. path(index: number, inclusive?: boolean): [Blot, number][];
  252. removeChild(child: Blot): void;
  253. unwrap(): void;
  254. }
  255. export declare class ParentBlot extends ShadowBlot implements Parent {
  256. /**
  257. * Whitelist array of Blots that can be direct children.
  258. */
  259. static allowedChildren?: BlotConstructor[];
  260. /**
  261. * Default child blot to be inserted if this blot becomes empty.
  262. */
  263. static defaultChild?: BlotConstructor;
  264. static uiClass: string;
  265. children: LinkedList<Blot>;
  266. domNode: HTMLElement;
  267. uiNode: HTMLElement | null;
  268. constructor(scroll: Root, domNode: Node);
  269. appendChild(other: Blot): void;
  270. attach(): void;
  271. attachUI(node: HTMLElement): void;
  272. /**
  273. * Called during construction, should fill its own children LinkedList.
  274. */
  275. build(): void;
  276. deleteAt(index: number, length: number): void;
  277. descendant<T extends Blot>(criteria: new (...args: any[]) => T, index: number): [T | null, number];
  278. descendant(criteria: (blot: Blot) => boolean, index: number): [Blot | null, number];
  279. descendants<T extends Blot>(criteria: new (...args: any[]) => T, index?: number, length?: number): T[];
  280. descendants(criteria: (blot: Blot) => boolean, index?: number, length?: number): Blot[];
  281. detach(): void;
  282. enforceAllowedChildren(): void;
  283. formatAt(index: number, length: number, name: string, value: any): void;
  284. insertAt(index: number, value: string, def?: any): void;
  285. insertBefore(childBlot: Blot, refBlot?: Blot | null): void;
  286. length(): number;
  287. moveChildren(targetParent: Parent, refNode?: Blot | null): void;
  288. optimize(context?: {
  289. [key: string]: any;
  290. }): void;
  291. path(index: number, inclusive?: boolean): [Blot, number][];
  292. removeChild(child: Blot): void;
  293. replaceWith(name: string | Blot, value?: any): Blot;
  294. split(index: number, force?: boolean): Blot | null;
  295. splitAfter(child: Blot): Parent;
  296. unwrap(): void;
  297. update(mutations: MutationRecord[], _context: {
  298. [key: string]: any;
  299. }): void;
  300. }
  301. export declare class Registry implements RegistryInterface {
  302. static blots: WeakMap<Node, Blot>;
  303. static find(node?: Node | null, bubble?: boolean): Blot | null;
  304. private attributes;
  305. private classes;
  306. private tags;
  307. private types;
  308. create(scroll: Root, input: Node | string | Scope, value?: any): Blot;
  309. find(node: Node | null, bubble?: boolean): Blot | null;
  310. query(query: string | Node | Scope, scope?: Scope): RegistryDefinition | null;
  311. register(...definitions: RegistryDefinition[]): RegistryDefinition[];
  312. }
  313. export declare type RegistryDefinition = Attributor | BlotConstructor;
  314. export declare interface RegistryInterface {
  315. create(scroll: Root, input: Node | string | Scope, value?: any): Blot;
  316. query(query: string | Node | Scope, scope: Scope): RegistryDefinition | null;
  317. register(...definitions: any[]): any;
  318. }
  319. export declare interface Root extends Parent {
  320. create(input: Node | string | Scope, value?: any): Blot;
  321. find(node: Node | null, bubble?: boolean): Blot | null;
  322. query(query: string | Node | Scope, scope?: Scope): RegistryDefinition | null;
  323. }
  324. export declare enum Scope {
  325. TYPE = 3,// 0011 Lower two bits
  326. LEVEL = 12,// 1100 Higher two bits
  327. ATTRIBUTE = 13,// 1101
  328. BLOT = 14,// 1110
  329. INLINE = 7,// 0111
  330. BLOCK = 11,// 1011
  331. BLOCK_BLOT = 10,// 1010
  332. INLINE_BLOT = 6,// 0110
  333. BLOCK_ATTRIBUTE = 9,// 1001
  334. INLINE_ATTRIBUTE = 5,// 0101
  335. ANY = 15
  336. }
  337. export declare class ScrollBlot extends ParentBlot implements Root {
  338. registry: Registry;
  339. static blotName: string;
  340. static defaultChild: typeof BlockBlot;
  341. static allowedChildren: BlotConstructor[];
  342. static scope: Scope;
  343. static tagName: string;
  344. observer: MutationObserver;
  345. constructor(registry: Registry, node: HTMLDivElement);
  346. create(input: Node | string | Scope, value?: any): Blot;
  347. find(node: Node | null, bubble?: boolean): Blot | null;
  348. query(query: string | Node | Scope, scope?: Scope): RegistryDefinition | null;
  349. register(...definitions: RegistryDefinition[]): RegistryDefinition[];
  350. build(): void;
  351. detach(): void;
  352. deleteAt(index: number, length: number): void;
  353. formatAt(index: number, length: number, name: string, value: any): void;
  354. insertAt(index: number, value: string, def?: any): void;
  355. optimize(context?: {
  356. [key: string]: any;
  357. }): void;
  358. optimize(mutations: MutationRecord[], context: {
  359. [key: string]: any;
  360. }): void;
  361. update(mutations?: MutationRecord[], context?: {
  362. [key: string]: any;
  363. }): void;
  364. }
  365. export declare class ShadowBlot implements Blot {
  366. scroll: Root;
  367. domNode: Node;
  368. static blotName: string;
  369. static className: string;
  370. static requiredContainer: BlotConstructor;
  371. static scope: Scope;
  372. static tagName: string | string[];
  373. static create(rawValue?: unknown): Node;
  374. prev: Blot | null;
  375. next: Blot | null;
  376. parent: Parent;
  377. get statics(): any;
  378. constructor(scroll: Root, domNode: Node);
  379. attach(): void;
  380. clone(): Blot;
  381. detach(): void;
  382. deleteAt(index: number, length: number): void;
  383. formatAt(index: number, length: number, name: string, value: any): void;
  384. insertAt(index: number, value: string, def?: any): void;
  385. isolate(index: number, length: number): Blot;
  386. length(): number;
  387. offset(root?: Blot): number;
  388. optimize(_context?: {
  389. [key: string]: any;
  390. }): void;
  391. remove(): void;
  392. replaceWith(name: string | Blot, value?: any): Blot;
  393. split(index: number, _force?: boolean): Blot | null;
  394. update(_mutations: MutationRecord[], _context: {
  395. [key: string]: any;
  396. }): void;
  397. wrap(name: string | Parent, value?: any): Parent;
  398. }
  399. export declare class StyleAttributor extends Attributor {
  400. static keys(node: HTMLElement): string[];
  401. add(node: HTMLElement, value: any): boolean;
  402. remove(node: HTMLElement): void;
  403. value(node: HTMLElement): any;
  404. }
  405. export declare class TextBlot extends LeafBlot implements Leaf {
  406. static readonly blotName = "text";
  407. static scope: Scope;
  408. static create(value: string): Text;
  409. static value(domNode: Text): string;
  410. domNode: Text;
  411. protected text: string;
  412. constructor(scroll: Root, node: Node);
  413. deleteAt(index: number, length: number): void;
  414. index(node: Node, offset: number): number;
  415. insertAt(index: number, value: string, def?: any): void;
  416. length(): number;
  417. optimize(context: {
  418. [key: string]: any;
  419. }): void;
  420. position(index: number, _inclusive?: boolean): [Node, number];
  421. split(index: number, force?: boolean): Blot | null;
  422. update(mutations: MutationRecord[], _context: {
  423. [key: string]: any;
  424. }): void;
  425. value(): string;
  426. }
  427. export { }