| 1 |
import { Plugin } from "@html_editor/plugin"; |
| 2 |
|
| 3 |
/** |
| 4 |
* @typedef {import("@html_editor/core/user_command_plugin").UserCommand} UserCommand |
| 5 |
* |
| 6 |
* @typedef {((url: string) => UserCommand)[]} paste_media_url_command_providers |
| 7 |
*/ |
| 8 |
|
| 9 |
export class MediaUrlPastePlugin extends Plugin { |
| 10 |
static id = "mediaUrlPaste"; |
| 11 |
static dependencies = ["link", "dom", "history", "powerbox"]; |
| 12 |
/** @type {import("plugins").EditorResources} */ |
| 13 |
resources = { |
| 14 |
paste_url_overrides: this.openPowerboxOnUrlPaste.bind(this), |
| 15 |
}; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param {string} text |
| 19 |
* @param {string} url |
| 20 |
*/ |
| 21 |
openPowerboxOnUrlPaste(text, url) { |
| 22 |
const commands = this.getResource("paste_media_url_command_providers") |
| 23 |
.map((provider) => provider(url)) |
| 24 |
.filter(Boolean); |
| 25 |
if (commands.length) { |
| 26 |
commands.push(this.dependencies.link.getPathAsUrlCommand(text, url)); |
| 27 |
const restoreSavepoint = this.dependencies.history.makeSavePoint(); |
| 28 |
// Open powerbox with commands to embed media or paste as link. |
| 29 |
// Insert URL as text, revert it later if a command is triggered. |
| 30 |
this.dependencies.dom.insert(text); |
| 31 |
this.dependencies.history.addStep(); |
| 32 |
this.dependencies.powerbox.openPowerbox({ commands, onApplyCommand: restoreSavepoint }); |
| 33 |
return true; |
| 34 |
} |
| 35 |
} |
| 36 |
} |