vue-codemirror icon indicating copy to clipboard operation
vue-codemirror copied to clipboard

How can i insert something in the cursor position thought JS

Open cmdparkour opened this issue 6 years ago • 7 comments

Hi guy! I have a question, how can i add something in the cursor in the codemirror I have a button, when i click it , it can give me a string like '

123456
' , how can i insert it in the cursor Thank for your watching!

cmdparkour avatar Aug 15 '19 07:08 cmdparkour

I know the answer about it ,it has q api ,please view this page: http://blog.sina.com.cn/s/blog_607787d30102xisw.html replaceSelection It's amazing to do it.

cmdparkour avatar Aug 15 '19 07:08 cmdparkour

I know the answer about it ,it has q api ,please view this page: http://blog.sina.com.cn/s/blog_607787d30102xisw.html replaceSelection It's amazing to do it.

unable to view page

saivikas-500 avatar Nov 23 '21 12:11 saivikas-500

I also encountered this problem. The address failed. Can you send it again? thank you

zhangsai521314 avatar Sep 21 '22 11:09 zhangsai521314

try it

<template>
  <codemirror
    ...
    @ready="handleReady"
    ...
  />
</template>

<script>
  import { defineComponent } from 'vue'
  import { Codemirror, shallowRef } from 'vue-codemirror'

  export default defineComponent({
    components: {
      Codemirror
    },
    setup() {

      const view = shallowRef();

      const handleReady = (payload) => {
        view.value = payload.view
      };

      //here
      //@param text string 
      const insertText = (text) => {
        const state = view.state;
        const ranges = state.selection.ranges;
        const cursor = ranges[0].anchor;

        view.value.dispatch({
            changes: { from: cursor, insert: text }
        });
      }

      return {
        handleReady
      }
    }
  })
</script>

GiveMeName avatar Nov 25 '22 06:11 GiveMeName