clipboard.mdx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ---
  2. title: Clipboard Module
  3. ---
  4. The Clipboard handles copy, cut and paste between Quill and external applications. A set of defaults exist to provide sane interpretation of pasted content, with the ability for further customization through matchers.
  5. The Clipboard interprets pasted HTML by traversing the corresponding DOM tree in [post-order](https://en.wikipedia.org/wiki/Tree_traversal#Post-order), building up a [Delta](/docs/delta/) representation of all subtrees. At each descendant node, matcher functions are called with the DOM Node and Delta interpretation so far, allowing the matcher to return a modified Delta interpretation.
  6. Familiarity and comfort with [Deltas](/docs/delta/) is necessary in order to effectively use matchers.
  7. ## API
  8. #### addMatcher
  9. Adds a custom matcher to the Clipboard. Matchers using `nodeType` are called first, in the order they were added, followed by matchers using a CSS `selector`, also in the order they were added. [`nodeType`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType) may be `Node.ELEMENT_NODE` or `Node.TEXT_NODE`.
  10. **Methods**
  11. ```javascript
  12. addMatcher(selector: String, (node: Node, delta: Delta) => Delta)
  13. addMatcher(nodeType: Number, (node: Node, delta: Delta) => Delta)
  14. ```
  15. **Examples**
  16. ```javascript
  17. quill.clipboard.addMatcher(Node.TEXT_NODE, function(node, delta) {
  18. return new Delta().insert(node.data);
  19. });
  20. // Interpret a <b> tag as bold
  21. quill.clipboard.addMatcher('B', function(node, delta) {
  22. return delta.compose(new Delta().retain(delta.length(), { bold: true }));
  23. });
  24. ```
  25. ### dangerouslyPasteHTML
  26. Inserts content represented by HTML snippet into editor at a given index. The snippet is interpreted by Clipboard [matchers](#addMatcher), which may not produce the exactly input HTML. If no insertion index is provided, the entire editor contents will be overwritten. The [source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`.
  27. Improper handling of HTML can lead to [cross site scripting (XSS)](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)) and failure to sanitize properly is both notoriously error-prone and a leading cause of web vulnerabilities. This method follows React's example and is aptly named to ensure the developer has taken the necessary precautions.
  28. **Methods**
  29. ```javascript
  30. dangerouslyPasteHTML(html: String, source: String = 'api')
  31. dangerouslyPasteHTML(index: Number, html: String, source: String = 'api')
  32. ```
  33. **Examples**
  34. ```javascript
  35. quill.setText('Hello!');
  36. quill.clipboard.dangerouslyPasteHTML(5, '&nbsp;<b>World</b>');
  37. // Editor is now '<p>Hello&nbsp;<strong>World</strong>!</p>';
  38. ```
  39. ## Configuration
  40. ### matchers
  41. An array of matchers can be passed into Clipboard's configuration options. These will be appended after Quill's own default matchers.
  42. ```javascript
  43. const quill = new Quill('#editor', {
  44. modules: {
  45. clipboard: {
  46. matchers: [
  47. ['B', customMatcherA],
  48. [Node.TEXT_NODE, customMatcherB]
  49. ]
  50. }
  51. }
  52. });
  53. ```