nhn / toast-ui.vue-editor

This repository is DEPRECATED! GO TO πŸ‘‰ https://github.com/nhn/tui.editor/tree/master/apps/vue-editor
MIT License
229 stars 47 forks source link

[Question] How could call the Vue method in addImageBlobHook #45

Closed sweetyuria closed 4 years ago

sweetyuria commented 4 years ago

Version

@toast-ui/vue-editor: 1.1.1 vue: 2.6.10

Test Environment

Chrome IOS

Current Behavior

<template>
  <tui-editor :options="editorOptions" :height="defaultHeight"
              mode="wysiwyg" :visible="true" ref="tuiEditor"
  />
</template>

<script>
import { Editor } from '@toast-ui/vue-editor'

export default {
  components: {
    'tui-editor': Editor
  },
  props: {
    defaultHeight: String
  },
  data: () => ({
    editorOptions: {
      minHeight: '200px',
      language: 'ko_KR',
      useCommandShortcut: true,
      useDefaultHTMLSanitizer: true,
      usageStatistics: false,
      hideModeSwitch: true,
      toolbarItems: [
        'heading',
        'bold',
        'italic',
        'divider',
        'hr',
        'quote',
        'divider',
        'image',
        'link',
        'divider'
      ],
      exts: [
        'colorSyntax'
      ],
      hooks: {
        addImageBlobHook (blob, callback) {
          let url = this.uploadFile(blob) // How can I call a function?
          callback.call('[image]', url) // It works.
          return true
        }
      }
    }
  }),
  methods: {
    uploadFile (blob) {
      const formData = new FormData()
      // blob blar blar blar
      this.axios.post('/api/external/fileUpload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.code !== 0) throw res.data.message
        return res.data.data.imageUrl
      }, () => alert('μ‹œμŠ€ν…œμ—μ„œ 였λ₯˜κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€.'))
        .catch(errorMsg => alert(errorMsg))
        .finally(() => {
          return false
        })
    },
    getHtml () {
      let html = this.$refs.tuiEditor.invoke('getHtml')
      return html
    },
    setHtml (obj) {
      this.$refs.tuiEditor.invoke('setHtml', obj)
    }
  }
}
</script>

<style scoped>

</style>

Expected Behavior

addImageBlobHook call vue method.

I'd like to call my Vue method. How could I access the Vue method?

vue μ΄ˆλ³΄λΌμ„œ μ–΄λ–»κ²Œ 접근할지 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€. 쒋은 에디터λ₯Ό λ§Œλ“€μ–΄ μ£Όμ–΄μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. 멋지고 ν•œκΈ€ ν˜Έν™˜ μž˜λœλ‹€λŠ” 에디터λ₯Ό μ“°κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. 이미지 μ—…λ‘œλ“œμ‹œ 훅도 μ œκ³΅ν•΄μ„œ λ”± ν•„μš”ν•©λ‹ˆλ‹€. 맀우 μ“°κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. λ„μ™€μ£Όμ„Έμš”.

sweetyuria commented 4 years ago
      hooks: {
        addImageBlobHook (blob, callback) {
          let fileUpload = (blob) => {
            const formData = new FormData()
            formData.append('file', blob)
            const axios = require('axios') // this way
            axios.post('/api/external/fileUpload', formData, {
              headers: {
                'Content-Type': 'multipart/form-data'
              }
            }).then(res => {
              if (res.data.code !== 0) throw res.data.message
              callback.call('[image]', res.data.data.imageUrl)
            }, () => alert('μ‹œμŠ€ν…œμ—μ„œ 였λ₯˜κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€. κ°œλ°œνŒ€μ— λ¬Έμ˜λ°”λžλ‹ˆλ‹€.'))
              .catch(errorMsg => alert(errorMsg))
          }

          fileUpload(blob, callback)

          return true
        }
      }