rayjohnttttt
关注
66202 号成员,2021-03-19 16:01:53 加入
410
个人主页 浏览
20h3m
在线时长
  • 如何做到代码块自定义渲染?

    2021-03-28 22:55

    你好,我看了 go 的实现,在不修改源码的情况下,还是无法做到自定义渲染代码块。

    比如以下:

    <pre class="line-numbers"><code>...</code></pre>
    
  • 请问在哪里设置编辑器初始值(setValue),after 也不成功?

    2021-03-19 21:09

    已解决。经过反复尝试,还是得用本地变量,就可以解决这个问题。我是在 vue 中使用的。不能直接 this.vditor.setValue.

          const vditor = new Vditor('vditor', {
            width: this.isMobile ? '100%' : '80%',
            height: '0',
            tab: '\t',
            counter: '999999',
            typewriterMode: true,
            mode: 'sv',
            preview: {
              delay: 100,
              show: !this.isMobile
            },
            after() {
              console.log('--- init content ---');
              if(vditor) {
                console.log('--- not empty ---');
                vditor.setValue('# 哈哈', false);
              } else {
                console.log('--- empty --');
              }
            }
          });
          this.contentEditor = vditor;
    

    成功 setValue。

    我猜测应该是跟 this 的生命周期有关系。

    ReactJs 可能也会遇到这个问题吧 😄 。

    但是我看另一个项目(使用 vditor 3.2.12 版本)使用了 vditor,他的代码如下,可以 setValue:

    https://github.com/lhlyu/petard (演示地址:https://lhlyu.github.io/petard/#/)

    export default {
      name: 'index',
      components: {
        Upload
      },
      watch: {
        $route (val, oldVal) {
          if (val.name !== 'article-edit') {
            return
          }
          this.loadArticle(val.query.id)
        }
      },
      data () {
        return {
          loading: false,
          dialogCoverVisible: false,
          contentEditor: null,
          categorys: null,
          req: {
            id: 0,
            title: '',
            summary: '',
            cover: '',
            top: 0,
            categoryId: null,
            tags: [],
            content: '',
            kind: 1,
            storeMode: 2,
            state: 1,
            commentState: 1
          }
        }
      },
      methods: {
        init () {
          this.initEdtor()
          this.loadCategorys()
          this.loadArticle(this.$route.query.id)
        },
        initEdtor (value) {
          this.contentEditor = new Vditor('vditor', {
            mode: 'sv',
            minHeight: 550,
            cache: {
              enable: true,
              id: 'petardEditor'
            }
          })
        },
        add () {
          this.req = {
            id: 0,
            title: '',
            summary: '',
            cover: '',
            top: 0,
            categoryId: null,
            tags: [],
            content: '',
            kind: 1,
            storeMode: 2,
            state: 1,
            commentState: 1
          }
          this.contentEditor.setValue('')
        },
        reset () {
          if (this.req.id === 0) {
            this.add()
            return
          }
          this.loadArticle(this.req.id)
        },
        async loadArticle (id) {
          if (!id) {
            return
          }
          this.loading = true
          const result = await this.$request.fetchArticle({
            id: id
          })
          if (result.code) {
            this.$message.warning(result.message)
            this.loading = false
            return
          }
          this.req = result.data
          this.contentEditor.setValue(this.req.content)
          this.loading = false
        },
        async loadCategorys () {
          this.loading = true
          const result = await this.$request.fetchCategorys({
            pageNum: 1,
            pageSize: 20
          })
          if (result.code) {
            this.categorys = []
            this.$message.warning(result.message)
            this.loading = false
            return
          }
          this.categorys = result.data.list
          this.loading = false
        },
        async save () {
          const length = this.contentEditor.getHTML().trim().length
          if (length === 0) {
            this.$message.warning('内容不能为空!')
          }
          if (this.isEmpty(this.req.title, '标题不能为空!')) {
            return
          }
          this.req.content = this.contentEditor.getValue().trim()
          this.loading = true
          const result = await this.$request.fetchEditArticle(this.req)
          if (result.code) {
            this.$message.warning(result.message)
            this.loading = false
            return
          }
          this.$message.success(result.message)
          this.loading = false
        },
        // ------------ handler -----------------
        handlerUpload (data) {
          if (!data) {
            return
          }
          this.req.cover = data.url
        }
      },
      computed: {
        ...mapState(['dict'])
      }
    }
    
  • 请问在哪里设置编辑器初始值(setValue),after 也不成功?

    2021-03-19 20:33

    观察到一个跟版本有关的奇怪现象:

    版本 3.2.12,在初始化之后,是可以 setValue 的,但是会报 render undefined 的错误。

    但这个版本的 after,setValue 的时候,vditor 还是空的。

    3.2.12 的代码:

      mounted() {
        // this.initVditor()
        this.initVditor();
        this.$nextTick(() => {
          this.isLoading = false
        })
    
        console.log('---- in mounted ---');
        if(this.contentEditor) {
          console.log('----- not empty -----');
          this.contentEditor.setValue('# 在mounted中, 哈哈', true);
        } else {
          console.log('----- empty -----');
        }
      },
    

    有可能是回归 bug 吗?

  • 请问在哪里设置编辑器初始值(setValue),after 也不成功?

    2021-03-19 19:30

    focus 注释掉了 还是不成功。

  • 请问在哪里设置编辑器初始值(setValue),after 也不成功?

    2021-03-19 17:52

    你好。我贴上代码了 能帮忙看看是为什么吗

  • 请问在哪里设置编辑器初始值(setValue),after 也不成功?

    2021-03-19 17:02

    已经是 new 里的方法,以下是完整代码:

      mounted() {
        this.initVditor()
        this.$nextTick(() => {
          this.isLoading = false
        })
      },
    
    initVditor() {
          const that = this;
          const options = {
            width: '100%',
            height: '0',
            tab: '\t',
            counter: '999999',
            typewriterMode: true,
            mode: 'sv',
            preview: {
              delay: 100,
              show: !this.isMobile
            },
            outline: true,
            upload: {
              max: 5 * 1024 * 1024,
              // linkToImgUrl: 'https://sm.ms/api/upload',
              handler(file) {
                let formData = new FormData();
                for (let i in file) {
                  formData.append('smfile', file[i]);
                }
                let request = new XMLHttpRequest()
                request.open('POST', 'https://sm.ms/api/upload')
                request.onload = that.onloadCallback;
                request.send(formData);
              }
            },
            after() {
              console.log('--- init content ----');
              // vditor.setValue('# 哈哈', true);
              if (this.vditor) {
                console.log('---- ok vditor --');
              } else {
                console.log('--- empty vditor ---');
              }
            }
          };
          this.vditor = new Vditor('vditor', options);
          this.vditor.focus();