api.mdx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. ---
  2. title: API
  3. ---
  4. <div className="table-of-contents">
  5. {data.api.map(({ hashes, title }) => (
  6. <nav key={title}>
  7. <h4 anchor="off">{title}</h4>
  8. <ul>
  9. {hashes.map(hash => (
  10. <li key={hash}>
  11. <a href={`#${hash.toLowerCase()}`}>
  12. {hash.replace('-experimental', '')}
  13. </a>
  14. </li>
  15. ))}
  16. </ul>
  17. </nav>
  18. ))}
  19. </div>
  20. ## Content
  21. ### deleteText
  22. Deletes text from the editor, returning a [Delta](/docs/delta/) representing the change. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  23. **Methods**
  24. ```typescript
  25. deleteText(index: number, length: number, source: string = 'api'): Delta
  26. ```
  27. **Example 1**
  28. ```typescript
  29. quill.deleteText(6, 4);
  30. ```
  31. **Example 2**
  32. <SandpackWithQuillTemplate
  33. beforeEditor={`
  34. <button id="deleteButton">Delete Text</button>
  35. <hr/>
  36. `}
  37. content={`
  38. <p>Hello, World!</p>
  39. `}
  40. files={{
  41. 'index.js': `
  42. const quill = new Quill('#editor', { theme: 'snow' });
  43. document.querySelector('#deleteButton').addEventListener('click', () => {
  44. quill.deleteText(5, 7);
  45. });
  46. `
  47. }}
  48. />
  49. ### getContents
  50. Retrieves contents of the editor, with formatting data, represented by a [Delta](/docs/delta/) object.
  51. **Methods**
  52. ```typescript
  53. getContents(index: number = 0, length: number = remaining): Delta
  54. ```
  55. **Examples**
  56. ```typescript
  57. const delta = quill.getContents();
  58. ```
  59. ### getLength
  60. Retrieves the length of the editor contents. Note even when Quill is empty, there is still a blank line represented by '\n', so `getLength` will return 1.
  61. **Methods**
  62. ```typescript
  63. getLength(): number
  64. ```
  65. **Examples**
  66. ```typescript
  67. const length = quill.getLength();
  68. ```
  69. ### getText
  70. Retrieves the string contents of the editor. Non-string content are omitted, so the returned string's length may be shorter than the editor's as returned by [`getLength`](#getlength). Note even when Quill is empty, there is still a blank line in the editor, so in these cases `getText` will return '\n'.
  71. The `length` parameter defaults to the length of the remaining document.
  72. **Methods**
  73. ```typescript
  74. getText(index: number = 0, length: number = remaining): string
  75. ```
  76. **Examples**
  77. ```typescript
  78. const text = quill.getText(0, 10);
  79. ```
  80. ### getSemanticHTML
  81. Get the HTML representation of the editor contents.
  82. This method is useful for exporting the contents of the editor in a format that can be used in other applications.
  83. The `length` parameter defaults to the length of the remaining document.
  84. **Methods**
  85. ```typescript
  86. getSemanticHTML(index: number = 0, length: number = remaining): string
  87. ```
  88. **Examples**
  89. ```typescript
  90. const html = quill.getSemanticHTML(0, 10);
  91. ```
  92. ### insertEmbed
  93. Insert embedded content into the editor, returning a [Delta](/docs/delta/) representing the change. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  94. **Methods**
  95. ```typescript
  96. insertEmbed(index: number, type: string, value: any, source: string = 'api'): Delta
  97. ```
  98. **Examples**
  99. ```typescript
  100. quill.insertEmbed(10, 'image', 'https://quilljs.com/images/cloud.png');
  101. ```
  102. ### insertText
  103. Inserts text into the editor, optionally with a specified format or multiple [formats](/docs/formats/). Returns a [Delta](/docs/delta/) representing the change. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  104. **Methods**
  105. ```typescript
  106. insertText(index: number, text: string, source: string = 'api'): Delta
  107. insertText(index: number, text: string, format: string, value: any,
  108. source: string = 'api'): Delta
  109. insertText(index: number, text: string, formats: { [String]: any },
  110. source: string = 'api'): Delta
  111. ```
  112. **Examples**
  113. ```typescript
  114. quill.insertText(0, 'Hello', 'bold', true);
  115. quill.insertText(5, 'Quill', {
  116. color: '#ffff00',
  117. italic: true,
  118. });
  119. ```
  120. ### setContents
  121. Overwrites editor with given contents. Contents should end with a [newline](/docs/delta/#line-formatting). Returns a Delta representing the change. This will be the same as the Delta passed in, if given Delta had no invalid operations. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  122. **Methods**
  123. ```typescript
  124. setContents(delta: Delta, source: string = 'api'): Delta
  125. ```
  126. **Examples**
  127. ```typescript
  128. quill.setContents([
  129. { insert: 'Hello ' },
  130. { insert: 'World!', attributes: { bold: true } },
  131. { insert: '\n' },
  132. ]);
  133. ```
  134. ### setText
  135. Sets contents of editor with given text, returning a [Delta](/docs/delta/) representing the change. Note Quill documents must end with a newline so one will be added for you if omitted. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  136. **Methods**
  137. ```typescript
  138. setText(text: string, source: string = 'api'): Delta
  139. ```
  140. **Examples**
  141. ```typescript
  142. quill.setText('Hello\n');
  143. ```
  144. ### updateContents
  145. Applies Delta to editor contents, returning a [Delta](/docs/delta/) representing the change. These Deltas will be the same if the Delta passed in had no invalid operations. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  146. **Methods**
  147. ```typescript
  148. updateContents(delta: Delta, source: string = 'api'): Delta
  149. ```
  150. **Examples**
  151. ```typescript
  152. // Assuming editor currently contains [{ insert: 'Hello World!' }]
  153. quill.updateContents(new Delta()
  154. .retain(6) // Keep 'Hello '
  155. .delete(5) // 'World' is deleted
  156. .insert('Quill')
  157. .retain(1, { bold: true }) // Apply bold to exclamation mark
  158. );
  159. // Editor should now be [
  160. // { insert: 'Hello Quill' },
  161. // { insert: '!', attributes: { bold: true} }
  162. // ]
  163. ```
  164. <Hint>
  165. This method updates the contents from the beginning, not from the current selection.
  166. Use `Delta#retain(length: number)` to skip the contents you wish to leave unchanged.
  167. </Hint>
  168. ## Formatting
  169. ### format
  170. Format text at user's current selection, returning a [Delta](/docs/delta/) representing the change. If the user's selection length is 0, i.e. it is a cursor, the format will be set active, so the next character the user types will have that formatting. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  171. **Methods**
  172. ```typescript
  173. format(name: string, value: any, source: string = 'api'): Delta
  174. ```
  175. **Examples**
  176. ```typescript
  177. quill.format('color', 'red');
  178. quill.format('align', 'right');
  179. ```
  180. ### formatLine
  181. Formats all lines in given range, returning a [Delta](/docs/delta/) representing the change. See [formats](/docs/formats/) for a list of available formats. Has no effect when called with inline formats. To remove formatting, pass `false` for the value argument. The user's selection may not be preserved. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  182. **Methods**
  183. ```typescript
  184. formatLine(index: number, length: number, source: string = 'api'): Delta
  185. formatLine(index: number, length: number, format: string, value: any,
  186. source: string = 'api'): Delta
  187. formatLine(index: number, length: number, formats: { [String]: any },
  188. source: string = 'api'): Delta
  189. ```
  190. **Examples**
  191. ```typescript
  192. quill.setText('Hello\nWorld!\n');
  193. quill.formatLine(1, 2, 'align', 'right'); // right aligns the first line
  194. quill.formatLine(4, 4, 'align', 'center'); // center aligns both lines
  195. ```
  196. ### formatText
  197. Formats text in the editor, returning a [Delta](/docs/delta/) representing the change. For line level formats, such as text alignment, target the newline character or use the [`formatLine`](#formatline) helper. See [formats](/docs/formats/) for a list of available formats. To remove formatting, pass `false` for the value argument. The user's selection may not be preserved. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  198. **Methods**
  199. ```typescript
  200. formatText(index: number, length: number, source: string = 'api'): Delta
  201. formatText(index: number, length: number, format: string, value: any,
  202. source: string = 'api'): Delta
  203. formatText(index: number, length: number, formats: { [String]: any },
  204. source: string = 'api'): Delta
  205. ```
  206. **Examples**
  207. ```typescript
  208. quill.setText('Hello\nWorld!\n');
  209. quill.formatText(0, 5, 'bold', true); // bolds 'hello'
  210. quill.formatText(0, 5, { // unbolds 'hello' and set its color to blue
  211. 'bold': false,
  212. 'color': 'rgb(0, 0, 255)'
  213. });
  214. quill.formatText(5, 1, 'align', 'right'); // right aligns the 'hello' line
  215. ```
  216. ### getFormat
  217. Retrieves common formatting of the text in the given range. For a format to be reported, all text within the range must have a truthy value. If there are different truthy values, an array with all truthy values will be reported. If no range is supplied, the user's current selection range is used. May be used to show which formats have been set on the cursor. If called with no arguments, the user's current selection range will be used.
  218. **Methods**
  219. ```typescript
  220. getFormat(range: Range = current): { [String]: any }
  221. getFormat(index: number, length: number = 0): { [String]: any }
  222. ```
  223. **Examples**
  224. ```typescript
  225. quill.setText('Hello World!');
  226. quill.formatText(0, 2, 'bold', true);
  227. quill.formatText(1, 2, 'italic', true);
  228. quill.getFormat(0, 2); // { bold: true }
  229. quill.getFormat(1, 1); // { bold: true, italic: true }
  230. quill.formatText(0, 2, 'color', 'red');
  231. quill.formatText(2, 1, 'color', 'blue');
  232. quill.getFormat(0, 3); // { color: ['red', 'blue'] }
  233. quill.setSelection(3);
  234. quill.getFormat(); // { italic: true, color: 'blue' }
  235. quill.format('strike', true);
  236. quill.getFormat(); // { italic: true, color: 'blue', strike: true }
  237. quill.formatLine(0, 1, 'align', 'right');
  238. quill.getFormat(); // { italic: true, color: 'blue', strike: true,
  239. // align: 'right' }
  240. ```
  241. ### removeFormat
  242. Removes all formatting and embeds within given range, returning a [Delta](/docs/delta/) representing the change. Line formatting will be removed if any part of the line is included in the range. The user's selection may not be preserved. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. Calls where the `source` is `"user"` when the editor is [disabled](#disable) are ignored.
  243. **Methods**
  244. ```typescript
  245. removeFormat(index: number, length: number, source: string = 'api'): Delta
  246. ```
  247. **Examples**
  248. ```typescript
  249. quill.setContents([
  250. { insert: 'Hello', { bold: true } },
  251. { insert: '\n', { align: 'center' } },
  252. { insert: { formula: 'x^2' } },
  253. { insert: '\n', { align: 'center' } },
  254. { insert: 'World', { italic: true }},
  255. { insert: '\n', { align: 'center' } }
  256. ]);
  257. quill.removeFormat(3, 7);
  258. // Editor contents are now
  259. // [
  260. // { insert: 'Hel', { bold: true } },
  261. // { insert: 'lo\n\nWo' },
  262. // { insert: 'rld', { italic: true }},
  263. // { insert: '\n', { align: 'center' } }
  264. // ]
  265. ```
  266. ## Selection
  267. ### getBounds
  268. Retrieves the pixel position (relative to the editor container) and dimensions of a selection at a given location. The user's current selection need not be at that index. Useful for calculating where to place tooltips.
  269. **Methods**
  270. ```typescript
  271. getBounds(index: number, length: number = 0):
  272. { left: number, top: number, height: number, width: number }
  273. ```
  274. **Examples**
  275. ```typescript
  276. quill.setText('Hello\nWorld\n');
  277. quill.getBounds(7); // Returns { height: 15, width: 0, left: 27, top: 31 }
  278. ```
  279. ### getSelection
  280. Retrieves the user's selection range, optionally to focus the editor first. Otherwise `null` may be returned if editor does not have focus.
  281. **Methods**
  282. ```typescript
  283. getSelection(focus = false): { index: number, length: number }
  284. ```
  285. **Examples**
  286. ```typescript
  287. const range = quill.getSelection();
  288. if (range) {
  289. if (range.length == 0) {
  290. console.log('User cursor is at index', range.index);
  291. } else {
  292. const text = quill.getText(range.index, range.length);
  293. console.log('User has highlighted: ', text);
  294. }
  295. } else {
  296. console.log('User cursor is not in editor');
  297. }
  298. ```
  299. ### setSelection
  300. Sets user selection to given range, which will also focus the editor. Providing `null` as the selection range will blur the editor. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`.
  301. **Methods**
  302. ```typescript
  303. setSelection(index: number, length: number = 0, source: string = 'api')
  304. setSelection(range: { index: number, length: number },
  305. source: string = 'api')
  306. ```
  307. **Examples**
  308. ```typescript
  309. quill.setSelection(0, 5);
  310. ```
  311. ### scrollSelectionIntoView
  312. Scroll the current selection into the visible area.
  313. If the selection is already visible, no scrolling will occur.
  314. <Hint>
  315. Quill calls this method automatically when [setSelection](#setselection) is called, unless the source is `"silent"`.
  316. </Hint>
  317. **Methods**
  318. ```typescript
  319. scrollSelectionIntoView();
  320. ```
  321. **Examples**
  322. ```typescript
  323. quill.scrollSelectionIntoView();
  324. ```
  325. ## Editor
  326. ### blur
  327. Removes focus from the editor.
  328. **Methods**
  329. ```typescript
  330. blur();
  331. ```
  332. **Examples**
  333. ```typescript
  334. quill.blur();
  335. ```
  336. ### disable
  337. Shorthand for [`enable(false)`](#enable).
  338. ### enable
  339. Set ability for user to edit, via input devices like the mouse or keyboard. Does not affect capabilities of API calls, when the `source` is `"api"` or `"silent"`.
  340. **Methods**
  341. ```typescript
  342. enable(enabled: boolean = true);
  343. ```
  344. **Examples**
  345. ```typescript
  346. quill.enable();
  347. quill.enable(false); // Disables user input
  348. ```
  349. ### focus
  350. Focuses the editor and restores its last range.
  351. **Methods**
  352. ```typescript
  353. focus(options: { preventScroll?: boolean } = {});
  354. ```
  355. **Examples**
  356. ```typescript
  357. // Focus the editor, and scroll the selection into view
  358. quill.focus();
  359. // Focus the editor, but don't scroll
  360. quill.focus({ preventScroll: true });
  361. ```
  362. ### hasFocus
  363. Checks if editor has focus. Note focus on toolbar, tooltips, does not count as the editor.
  364. **Methods**
  365. ```typescript
  366. hasFocus(): Boolean
  367. ```
  368. **Examples**
  369. ```typescript
  370. quill.hasFocus();
  371. ```
  372. ### update
  373. Synchronously check editor for user updates and fires events, if changes have occurred. Useful for collaborative use cases during conflict resolution requiring the latest up to date state. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`.
  374. **Methods**
  375. ```typescript
  376. update(((source: string) = 'user'));
  377. ```
  378. **Examples**
  379. ```typescript
  380. quill.update();
  381. ```
  382. ### scrollRectIntoView #experimental
  383. Scrolls the editor to the given pixel position.
  384. [`scrollSelectionIntoView`](#scrollselectionintoview) is implemented by calling this method with the bounds of the current selection.
  385. ```typescript
  386. scrollRectIntoView(bounds: {
  387. top: number;
  388. right: number;
  389. bottom: number;
  390. left: number;
  391. });
  392. ```
  393. **Example 1**
  394. ```typescript
  395. // Scroll the editor to reveal the range of { index: 20, length: 5 }
  396. const bounds = this.selection.getBounds(20, 5);
  397. if (bounds) {
  398. quill.scrollRectIntoView(bounds);
  399. }
  400. ```
  401. **Example 2**
  402. <SandpackWithQuillTemplate
  403. files={{
  404. 'index.js': `
  405. let text = "";
  406. for (let i = 0; i < 100; i += 1) {
  407. text += \`line \${i + 1}\\n\`;
  408. }
  409. const quill = new Quill('#editor', { theme: 'snow' });
  410. quill.setText(text);
  411. const target = 'line 50';
  412. const bounds = quill.selection.getBounds(
  413. text.indexOf(target),
  414. target.length
  415. );
  416. if (bounds) {
  417. quill.scrollRectIntoView(bounds);
  418. }
  419. `
  420. }}
  421. />
  422. ## Events
  423. ### text-change
  424. Emitted when the contents of Quill have changed. Details of the change, representation of the editor contents before the change, along with the source of the change are provided. The source will be `"user"` if it originates from the users. For example:
  425. - User types into the editor
  426. - User formats text using the toolbar
  427. - User uses a hotkey to undo
  428. - User uses OS spelling correction
  429. Changes may occur through an API but as long as they originate from the user, the provided source should still be `"user"`. For example, when a user clicks on the toolbar, technically the toolbar module calls a Quill API to effect the change. But source is still `"user"` since the origin of the change was the user's click.
  430. APIs causing text to change may also be called with a `"silent"` source, in which case `text-change` will not be emitted. This is not recommended as it will likely break the undo stack and other functions that rely on a full record of text changes.
  431. Changes to text may cause changes to the selection (ex. typing advances the cursor), however during the `text-change` handler, the selection is not yet updated, and native browser behavior may place it in an inconsistent state. Use [`selection-change`](#selection-change) or [`editor-change`](#editor-change) for reliable selection updates.
  432. **Callback Signature**
  433. ```typescript
  434. handler(delta: Delta, oldContents: Delta, source: string)
  435. ```
  436. **Examples**
  437. ```typescript
  438. quill.on('text-change', (delta, oldDelta, source) => {
  439. if (source == 'api') {
  440. console.log('An API call triggered this change.');
  441. } else if (source == 'user') {
  442. console.log('A user action triggered this change.');
  443. }
  444. });
  445. ```
  446. ### selection-change
  447. Emitted when a user or API causes the selection to change, with a range representing the selection boundaries. A null range indicates selection loss (usually caused by loss of focus from the editor). You can also use this event as a focus change event by just checking if the emitted range is null or not.
  448. APIs causing the selection to change may also be called with a `"silent"` source, in which case `selection-change` will not be emitted. This is useful if `selection-change` is a side effect. For example, typing causes the selection to change but would be very noisy to also emit a `selection-change` event on every character.
  449. **Callback Signature**
  450. ```typescript
  451. handler(range: { index: number, length: number },
  452. oldRange: { index: number, length: number },
  453. source: string)
  454. ```
  455. **Examples**
  456. ```typescript
  457. quill.on('selection-change', (range, oldRange, source) => {
  458. if (range) {
  459. if (range.length == 0) {
  460. console.log('User cursor is on', range.index);
  461. } else {
  462. const text = quill.getText(range.index, range.length);
  463. console.log('User has highlighted', text);
  464. }
  465. } else {
  466. console.log('Cursor not in the editor');
  467. }
  468. });
  469. ```
  470. ### editor-change
  471. Emitted when either `text-change` or `selection-change` would be emitted, even when the source is `"silent"`. The first parameter is the event name, either `text-change` or `selection-change`, followed by the arguments normally passed to those respective handlers.
  472. **Callback Signature**
  473. ```typescript
  474. handler(name: string, ...args)
  475. ```
  476. **Examples**
  477. ```typescript
  478. quill.on('editor-change', (eventName, ...args) => {
  479. if (eventName === 'text-change') {
  480. // args[0] will be delta
  481. } else if (eventName === 'selection-change') {
  482. // args[0] will be old range
  483. }
  484. });
  485. ```
  486. ### on
  487. Adds event handler. See [text-change](#text-change) or [selection-change](#selection-change) for more details on the events themselves.
  488. **Methods**
  489. ```typescript
  490. on(name: string, handler: Function): Quill
  491. ```
  492. **Examples**
  493. ```typescript
  494. quill.on('text-change', () => {
  495. console.log('Text change!');
  496. });
  497. ```
  498. ### once
  499. Adds handler for one emission of an event. See [text-change](#text-change) or [selection-change](#selection-change) for more details on the events themselves.
  500. **Methods**
  501. ```typescript
  502. once(name: string, handler: Function): Quill
  503. ```
  504. **Examples**
  505. ```typescript
  506. quill.once('text-change', () => {
  507. console.log('First text change!');
  508. });
  509. ```
  510. ### off
  511. Removes event handler.
  512. **Methods**
  513. ```typescript
  514. off(name: string, handler: Function): Quill
  515. ```
  516. **Examples**
  517. ```typescript
  518. function handler() {
  519. console.log('Hello!');
  520. }
  521. quill.on('text-change', handler);
  522. quill.off('text-change', handler);
  523. ```
  524. ## Model
  525. ### find
  526. Static method returning the Quill or [Blot](https://github.com/quilljs/parchment) instance for the given DOM node. In the latter case, passing in true for the `bubble` parameter will search up the given DOM's ancestors until it finds a corresponding [Blot](https://github.com/quilljs/parchment).
  527. **Methods**
  528. ```typescript
  529. Quill.find(domNode: Node, bubble: boolean = false): Blot | Quill
  530. ```
  531. **Examples**
  532. ```typescript
  533. const container = document.querySelector('#container');
  534. const quill = new Quill(container);
  535. console.log(Quill.find(container) === quill); // Should be true
  536. quill.insertText(0, 'Hello', 'link', 'https://world.com');
  537. const linkNode = document.querySelector('#container a');
  538. const linkBlot = Quill.find(linkNode);
  539. // Find Quill instance from a blot
  540. console.log(Quill.find(linkBlot.scroll.domNode.parentElement));
  541. ```
  542. ### getIndex
  543. Returns the distance between the beginning of document to the occurrence of the given [Blot](https://github.com/quilljs/parchment).
  544. **Methods**
  545. ```typescript
  546. getIndex(blot: Blot): number
  547. ```
  548. **Examples**
  549. ```typescript
  550. let [line, offset] = quill.getLine(10);
  551. let index = quill.getIndex(line); // index + offset should == 10
  552. ```
  553. ### getLeaf
  554. Returns the leaf [Blot](https://github.com/quilljs/parchment) at the specified index within the document.
  555. **Methods**
  556. ```typescript
  557. getLeaf(index: number): Blot
  558. ```
  559. **Examples**
  560. ```typescript
  561. quill.setText('Hello Good World!');
  562. quill.formatText(6, 4, 'bold', true);
  563. let [leaf, offset] = quill.getLeaf(7);
  564. // leaf should be a Text Blot with value "Good"
  565. // offset should be 1, since the returned leaf started at index 6
  566. ```
  567. ### getLine
  568. Returns the line [Blot](https://github.com/quilljs/parchment) at the specified index within the document.
  569. **Methods**
  570. ```typescript
  571. getLine(index: number): [Blot, Number]
  572. ```
  573. **Examples**
  574. ```typescript
  575. quill.setText('Hello\nWorld!');
  576. let [line, offset] = quill.getLine(7);
  577. // line should be a Block Blot representing the 2nd "World!" line
  578. // offset should be 1, since the returned line started at index 6
  579. ```
  580. ### getLines
  581. Returns the lines contained within the specified location.
  582. **Methods**
  583. ```typescript
  584. getLines(index: number = 0, length: number = remaining): Blot[]
  585. getLines(range: Range): Blot[]
  586. ```
  587. **Examples**
  588. ```typescript
  589. quill.setText('Hello\nGood\nWorld!');
  590. quill.formatLine(1, 1, 'list', 'bullet');
  591. let lines = quill.getLines(2, 5);
  592. // array with a ListItem and Block Blot,
  593. // representing the first two lines
  594. ```
  595. ## Extension
  596. ### debug
  597. Static method enabling logging messages at a given level: `'error'`, `'warn'`, `'log'`, or `'info'`. Passing `true` is equivalent to passing `'log'`. Passing `false` disables all messages.
  598. **Methods**
  599. ```typescript
  600. Quill.debug(level: string | Boolean)
  601. ```
  602. **Examples**
  603. ```typescript
  604. Quill.debug('info');
  605. ```
  606. ### import
  607. Static method returning Quill library, format, module, or theme. In general the path should map exactly to Quill source code directory structure. Unless stated otherwise, modification of returned entities may break required Quill functionality and is strongly discouraged.
  608. **Methods**
  609. ```typescript
  610. Quill.import(path): any
  611. ```
  612. **Examples**
  613. ```typescript
  614. const Parchment = Quill.import('parchment');
  615. const Delta = Quill.import('delta');
  616. const Toolbar = Quill.import('modules/toolbar');
  617. const Link = Quill.import('formats/link');
  618. // Similar to ES6 syntax `import Link from 'quill/formats/link';`
  619. ```
  620. <Hint>
  621. Don't confuse this with the [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) keyword for ECMAScript modules.
  622. `Quill.import()` doesn't load scripts over the network, it just returns the corresponding module from the Quill library without causing any side-effects.
  623. </Hint>
  624. ### register
  625. Registers a module, theme, or format(s), making them available to be added to an editor. Can later be retrieved with [`Quill.import`](/docs/api/#import). Use the path prefix of `'formats/'`, `'modules/'`, or `'themes/'` for registering formats, modules or themes, respectively. For formats specifically there is a shorthand to just pass in the format directly and the path will be autogenerated. Will overwrite existing definitions with the same path.
  626. **Methods**
  627. ```typescript
  628. Quill.register(format: Attributor | BlotDefinintion, supressWarning: Boolean = false)
  629. Quill.register(path: string, def: any, supressWarning: Boolean = false)
  630. Quill.register(defs: { [String]: any }, supressWarning: Boolean = false)
  631. ```
  632. **Examples**
  633. ```typescript
  634. const Module = Quill.import('core/module');
  635. class CustomModule extends Module {}
  636. Quill.register('modules/custom-module', CustomModule);
  637. ```
  638. ```typescript
  639. Quill.register({
  640. 'formats/custom-format': CustomFormat,
  641. 'modules/custom-module-a': CustomModuleA,
  642. 'modules/custom-module-b': CustomModuleB,
  643. });
  644. Quill.register(CustomFormat);
  645. // You cannot do Quill.register(CustomModuleA); as CustomModuleA is not a format
  646. ```
  647. ### addContainer
  648. Adds and returns a container element inside the Quill container, sibling to the editor itself. By convention, Quill modules should have a class name prefixed with `ql-`. Optionally include a refNode where container should be inserted before.
  649. **Methods**
  650. ```typescript
  651. addContainer(className: string, refNode?: Node): Element
  652. addContainer(domNode: Node, refNode?: Node): Element
  653. ```
  654. **Examples**
  655. ```typescript
  656. const container = quill.addContainer('ql-custom');
  657. ```
  658. ### getModule
  659. Retrieves a module that has been added to the editor.
  660. **Methods**
  661. ```typescript
  662. getModule(name: string): any
  663. ```
  664. **Examples**
  665. ```typescript
  666. const toolbar = quill.getModule('toolbar');
  667. ```