Monaco-editor: Monaco paste event

Created on 10 Aug 2016  路  13Comments  路  Source: microsoft/monaco-editor

Hey, is there support in Monaco for a paste event which can be used to set a custom callback?
Or should we instead add an event listener on Monaco's container element?

Cheers,
Tim

Most helpful comment

        window.addEventListener('paste', (e) => {
          if (this.editor.isFocused()) {
            let selection = this.editor.getSelection()
            let items = e.clipboardData.items
            for (let i = 0; i < items.length; i++) {
              let matches = items[i].type.match(/^image\/(png|jpg|jpeg|gif)$/i)
              if (matches) {
                var blob = items[i].getAsFile()
                const formData = new FormData()
                formData.append('file', blob);
                formData.append('token', this.$server.qiniu.image_uptoken)
                axios.post(this.$server.qiniu.uphost, formData).then((e) => {
                  let {data: {hash, key}} = e
                  this.editor.executeEdits("", [
                    {
                      range: new monaco.Range(selection.endLineNumber, selection.endColumn, selection.endLineNumber, selection.endColumn),
                      text: `![鍥剧墖](//${this.$server.qiniu.host}/${key})`
                    }
                  ])
                  let {endLineNumber, endColumn} = this.editor.getSelection()
                  this.editor.setPosition({lineNumber: endLineNumber, column: endColumn})
                }, () => {
                  this.$message.error('涓婁紶澶辫触锛屽彲鑳芥槸鍥犱负鏂囦欢澶ぇ')
                })
              }
            }
          }
        })

@k8w It may help.

All 13 comments

@timjl What sort of thing do you want to do on the paste event?

Internally, the paste event is captured by the editor and the text inside the event.clipboardData.getData('text/plain') is "pasted" in the editor. There are also fallback cases for browsers that don't support event.clipboardData (by scanning the <textarea>).

For example, I want to support paste-to-upload in my editor. @alexandrudima

@FrankFang any good solution for this?

@alexandrudima Truly I can listen paste event on wrapper element.
But how to prevent monaco default paste action?

For example, I want to achieve when you paste AAA, and actually insert BBB

        window.addEventListener('paste', (e) => {
          if (this.editor.isFocused()) {
            let selection = this.editor.getSelection()
            let items = e.clipboardData.items
            for (let i = 0; i < items.length; i++) {
              let matches = items[i].type.match(/^image\/(png|jpg|jpeg|gif)$/i)
              if (matches) {
                var blob = items[i].getAsFile()
                const formData = new FormData()
                formData.append('file', blob);
                formData.append('token', this.$server.qiniu.image_uptoken)
                axios.post(this.$server.qiniu.uphost, formData).then((e) => {
                  let {data: {hash, key}} = e
                  this.editor.executeEdits("", [
                    {
                      range: new monaco.Range(selection.endLineNumber, selection.endColumn, selection.endLineNumber, selection.endColumn),
                      text: `![鍥剧墖](//${this.$server.qiniu.host}/${key})`
                    }
                  ])
                  let {endLineNumber, endColumn} = this.editor.getSelection()
                  this.editor.setPosition({lineNumber: endLineNumber, column: endColumn})
                }, () => {
                  this.$message.error('涓婁紶澶辫触锛屽彲鑳芥槸鍥犱负鏂囦欢澶ぇ')
                })
              }
            }
          }
        })

@k8w It may help.

"Truly I can listen paste event on wrapper element.
But how to prevent monaco default paste action?"
@k8w Have you found any solution to preventing the default action?馃槉

@FrankFang how to preventDefault from event ? For example, I copied a picture and paste it on the editor, found that the name was entered into the editor.

@lilijialiang It happens to me too, I have no idea how to fix it.

@FrankFang this is my way

this.editor.onDidPaste( ( e ) => {
    this.pastePosition = e;
} )
// in paste event 
const selection = this.editor.getSelection( );

const range = new monaco.Range(
    this.pastePosition.startLineNumber || selection.endLineNumber,
    this.pastePosition.startColumn || selection.endColumn,
    this.pastePosition.endLineNumber || selection.endLineNumber,
    this.pastePosition.endColumn || selection.endColumn,
);

this.editor.executeEdits( '', [ { range, text: '' } ] );

Yeah, you remove the file name...
It's better if we can prevent the default output.

@FrankFang I agree, but I have not found a feasible method :(

Ctrl+Z the output you removed will comeout again.

@FrankFang this is my way

this.editor.onDidPaste( ( e ) => {
    this.pastePosition = e;
} )
// in paste event 
const selection = this.editor.getSelection( );

const range = new monaco.Range(
    this.pastePosition.startLineNumber || selection.endLineNumber,
    this.pastePosition.startColumn || selection.endColumn,
    this.pastePosition.endLineNumber || selection.endLineNumber,
    this.pastePosition.endColumn || selection.endColumn,
);

this.editor.executeEdits( '', [ { range, text: '' } ] );

May be linked to #791

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chengtie picture chengtie  路  3Comments

aarinsmith picture aarinsmith  路  3Comments

galyech picture galyech  路  3Comments

robclive picture robclive  路  3Comments

andreymarchenko picture andreymarchenko  路  3Comments