Hello Sam,
I'm not sure if attaching from files is easier to implement; anyway, today I've created a prototype plugin that implements inserting images from clipboard, and you can try an early version below. I think it's a good idea to add this functionality to the core, but it requires some more testing and adjusting first (handling ctrl+z, extendable storage which can be in a file, or in base64, or in a separate tiddler using base64, etc). After installation, copy an image and press ctrl+v in a tiddler editor to get [img[...]] inserted.
Best regards,
Yakov.
PS viva ChatGPT, it hugely accelerated creating this one, did this in an hour or a half :)
/***
|Name |AttachImagePlugin|
|Version|0.2.2|
|Author |Yakov Litvin|
***/
//{{{
config.macros.attachImage = {
fileToBase64: function(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result)
reader.onerror = error => reject(error)
reader.readAsDataURL(file)
})
},
insertImageBase64MarkupOnPaste: async function(event) {
const imgItem = Array.from(event.clipboardData.items)
.find(item => item.type.startsWith('image'))
if(!imgItem) return
const file = imgItem.getAsFile()
const base64Data = await config.macros.attachImage.fileToBase64(file)
const imgMarkup = `[img[${base64Data}]]`
const editor = event.target
const currentText = editor.value
const startPos = editor.selectionStart
const endPos = editor.selectionEnd
const textBefore = currentText.substring(0, startPos)
const textAfter = currentText.substring(endPos, currentText.length)
editor.value = textBefore + imgMarkup + textAfter
const cursorPosition = startPos + imgMarkup.length
editor.selectionStart = editor.selectionEnd = cursorPosition
},
init: function() {
const orig_editHandler = config.macros.edit.handler
config.macros.edit.handler = function() {
const editor = orig_editHandler.apply(this, arguments)
editor.addEventListener('paste', config.macros.attachImage.insertImageBase64MarkupOnPaste)
return editor
}
}
}
//}}}