MCPcopy Create free account
hub / github.com/editablejs/editable / withImage

Function withImage

packages/plugins/image/src/plugin/with-image.tsx:26–212  ·  view source on GitHub ↗
(editor: T, options: ImageOptions = {})

Source from the content-addressed store, hash-verified

24}
25
26export 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 })

Callers 1

withPluginsFunction · 0.85

Calls 10

setOptionsFunction · 0.90
withHistoryProtocolFunction · 0.90
openFileDialogFunction · 0.90
insertImageFunction · 0.90
uploadImageFunction · 0.90
readImageElementFunction · 0.90
rotateImgWithCanvasFunction · 0.90
withShortcutsFunction · 0.90
insertImagesFunction · 0.85
focusMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…