(editor: T, options: ImageOptions = {})
| 24 | } |
| 25 | |
| 26 | export const withImage = <T extends Editable>(editor: T, options: ImageOptions = {}) => { |
| 27 | const newEditor = editor as T & ImageEditor |
| 28 | |
| 29 | setOptions(newEditor, options) |
| 30 | |
| 31 | const { isInline, isVoid } = newEditor |
| 32 | const historyProtocol = withHistoryProtocol(newEditor) |
| 33 | const { capture } = historyProtocol |
| 34 | |
| 35 | historyProtocol.capture = (op: Operation) => { |
| 36 | if (op.type === 'set_node') { |
| 37 | const { path, newProperties } = op |
| 38 | if (Editor.hasPath(editor, path)) { |
| 39 | const image = Editor.node(editor, path) |
| 40 | if (Image.isImage(image[0])) { |
| 41 | const prop = newProperties as Partial<Image> |
| 42 | if (prop.url || prop.state || prop.percentage) { |
| 43 | return false |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | return capture(op) |
| 49 | } |
| 50 | |
| 51 | const { locale: localeOptions = {} } = options |
| 52 | Locale.setLocale(newEditor, locale, localeOptions) |
| 53 | |
| 54 | Slot.mount(newEditor, ImageViewer) |
| 55 | |
| 56 | newEditor.isInline = element => { |
| 57 | return ImageEditor.isImage(newEditor, element) || isInline(element) |
| 58 | } |
| 59 | |
| 60 | newEditor.isVoid = element => { |
| 61 | return ImageEditor.isImage(newEditor, element) || isVoid(element) |
| 62 | } |
| 63 | |
| 64 | const insertImages = (files: (File | string)[]) => { |
| 65 | Promise.all( |
| 66 | files.map(file => |
| 67 | typeof file === 'string' ? Promise.resolve(file) : readImageFileInfo(file), |
| 68 | ), |
| 69 | ).then(items => { |
| 70 | items.forEach(item => { |
| 71 | if (!item) return |
| 72 | if (typeof item === 'string') return newEditor.insertImage({ file: item }) |
| 73 | const { url, file, width, height } = item |
| 74 | const path = insertImage(newEditor, { |
| 75 | url, |
| 76 | width, |
| 77 | height, |
| 78 | name: file.name, |
| 79 | state: 'uploading', |
| 80 | }) |
| 81 | uploadImage(editor, path, file).then(() => { |
| 82 | URL.revokeObjectURL(url) |
| 83 | }) |
no test coverage detected
searching dependent graphs…