|
| 1 | +import Quill, { Delta } from "quill"; |
| 2 | +import Clipboard from "quill/modules/clipboard"; |
| 3 | + |
| 4 | +export default class CustomClipboard extends Clipboard { |
| 5 | + constructor(quill: Quill, options: any) { |
| 6 | + super(quill, options); |
| 7 | + |
| 8 | + // remove default list matchers for ol and ul |
| 9 | + this.matchers = this.matchers.filter(matcher => matcher[0] !== "ol, ul"); |
| 10 | + |
| 11 | + // add custom list matchers for ol and ul to allow custom list types (lower-alpha, lower-roman, etc.) |
| 12 | + this.addMatcher("ol, ul", (node, delta) => { |
| 13 | + const format = "list"; |
| 14 | + let list = "ordered"; |
| 15 | + const element = node as HTMLUListElement; |
| 16 | + const checkedAttr = element.getAttribute("data-checked"); |
| 17 | + if (checkedAttr) { |
| 18 | + list = checkedAttr === "true" ? "checked" : "unchecked"; |
| 19 | + } else { |
| 20 | + const listStyleType = element.style.listStyleType; |
| 21 | + if (listStyleType) { |
| 22 | + if (listStyleType === "disc") { |
| 23 | + // disc is standard list type, convert to bullet |
| 24 | + list = "bullet"; |
| 25 | + } else if (listStyleType === "decimal") { |
| 26 | + // list decimal type is dependant on indent level, convert to standard ordered list |
| 27 | + list = "ordered"; |
| 28 | + } else { |
| 29 | + list = listStyleType; |
| 30 | + } |
| 31 | + } else { |
| 32 | + list = element.tagName === "OL" ? "ordered" : "bullet"; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // apply list format to delta |
| 37 | + return delta.reduce((newDelta, op) => { |
| 38 | + if (!op.insert) return newDelta; |
| 39 | + if (op.attributes && op.attributes[format]) { |
| 40 | + return newDelta.push(op); |
| 41 | + } |
| 42 | + const formats = list ? { [format]: list } : {}; |
| 43 | + |
| 44 | + return newDelta.insert(op.insert, { ...formats, ...op.attributes }); |
| 45 | + }, new Delta()); |
| 46 | + }); |
| 47 | + } |
| 48 | +} |
0 commit comments