Milkdown / milkdown

🍼 Plugin driven WYSIWYG markdown editor framework.
https://milkdown.dev
MIT License
8.99k stars 411 forks source link

[Feature] Copy to clipboard functionality for Code Block component #1500

Open Jettonn opened 2 months ago

Jettonn commented 2 months ago

Initial checklist

Problem

Code Block component is really useful, but since it's created not to be extended by the user it's hard to add a copy-to-clipboard functionality to the component (although you can change the icons, buttons, etc for language selector).

Solution

It would be handy to be able to add functionalities yourself (extend the components) or just support copy to the clipboard button.

Alternatives

Let users extend milkdown components and plugins

sensiblearts commented 2 months ago

I'm trying to copy the current selection as markdown.

     theEditor.editor.action((ctx: Ctx) => {
            const editorView = ctx.get(editorViewCtx);
            let selection = editorView.state.tr.selection;
            // now what...?
            // I searched through `selection`'s fields, recursively,
            // but could not find a method to get the markdown.      
    });

Perhaps, something like selection.getMarkdown() would solve mine and would subsume your code block copy?

Then it's just a matter of navigator.clipboard.write(), isn't it?