Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report]setMarkdown内容不可编辑 #997

Open
1 of 3 tasks
WindyByte opened this issue Dec 10, 2024 · 3 comments
Open
1 of 3 tasks

[Bug Report]setMarkdown内容不可编辑 #997

WindyByte opened this issue Dec 10, 2024 · 3 comments
Labels
🐞bug Something isn't working pending-no-confirmed The direction of this issue has not yet been determined.

Comments

@WindyByte
Copy link

Prerequisites

Version

v0.8.52

Reproduction Link

No response

Describe the Bug

使用setMarkdown的内容不可编辑,但是其余的位置可以插入和编辑,求助!!
全局Control+X之后再粘贴,就可以输入了。。

System Information

chrome

Contributing

Yes, I would like to submit a PR for this issue.

@WindyByte WindyByte added pending-no-confirmed The direction of this issue has not yet been determined. 🐞bug Something isn't working labels Dec 10, 2024
@sunsonliu
Copy link
Collaborator

奇怪,没有重现出来,可以尝试在setMarkdown()之后,调用cherryObj.editor.editor.refresh() 这个方法刷新下编辑区再试试

@WindyByte
Copy link
Author

奇怪,没有重现出来,可以尝试在setMarkdown()之后,调用cherryObj.editor.editor.refresh() 这个方法刷新下编辑区再试试

我选择编辑区,没有光标,但是可以全选。使用this.cherryInstancePreviewer.editor.editor.refresh();也没作用呜呜呜,点击编辑区的时候,控制台会报如下错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'length')
at Jn2 (cherry-markdown.js?v=2131856f:3263:40)
at nr2 (cherry-markdown.js?v=2131856f:3283:17)
at Tr2 (cherry-markdown.js?v=2131856f:3439:17)
at kr2 (cherry-markdown.js?v=2131856f:3415:19)
at Fr2 (cherry-markdown.js?v=2131856f:3534:22)
at Ps2.bs2 (cherry-markdown.js?v=2131856f:5586:21)
at HTMLDivElement. (cherry-markdown.js?v=2131856f:3927:22)

如果将鼠标光标放到文本的最后,就会出现光标闪烁,也不会报错。参照之前的issue,我已经将cherry实例换成全局单例了,但还是不行呜呜呜

<template>
  <div class="article-container">
    <!-- 文章标题 -->
    <div class="article-header">
      <h1 v-if="article">{{ article.title }}</h1>
      <p v-if="loading" class="loading">加载中...</p>
      <p v-if="error" class="error">加载失败,请稍后再试。</p>
    </div>

    <!-- 文章内容 (Markdown 渲染) -->
    <div id="markdown-container"></div>
  </div>
</template>

<script>
import api from "@/api/article.js"
import "@/assets/article_detail.css";
import 'cherry-markdown/src/sass/themes/diy.scss'
import 'cherry-markdown/dist/cherry-markdown.css';
import {getCherryInstance} from "@/utils/cherryMarkdown.js";

export default {
  data() {
    return {
      article: null, // 存储文章数据
      loading: true, // 加载状态
      error: false, // 错误状态
      cherryInstancePreviewer: null, // Cherry 实例
    };
  },
  async created() {

  },
  async mounted() {
    this.cherryInstancePreviewer = getCherryInstance();
    const articleID = this.$route.query.articleID;
    if (articleID) {
      await this.fetchArticle(articleID);
      console.info("created() -> fetchArticle(articleID =", articleID, ")");
    } else {
      this.error = true;
    }
  },
  methods: {
    async fetchArticle(articleID) {
      this.loading = true;
      try {
        const response = await fetch(api.articleDetails + `?articleID=${articleID}`);
        if (!response.ok) throw new Error('Failed to fetch article.');
        const result = JSON.parse(await response.text()); // 解析 JSON
        console.log("Parsed JSON:", result);
        this.article = result.data
        console.log(this.result);
        // 解析 Markdown
        if (!this.cherryInstancePreviewer) console.info("this.cherryInstancePreviewer failed.")
        if (this.cherryInstancePreviewer && this.article.content) {
          this.cherryInstancePreviewer.setMarkdown(this.article.content, true);
          this.cherryInstancePreviewer.editor.editor.refresh();
          this.cherryInstancePreviewer.editor.editor.setCursor(this.cherryInstancePreviewer.getMarkdown().length)
          this.cherryInstancePreviewer.switchModel('edit&preview');
          // this.cherryInstancePreviewer.switchModel('previewOnly');
          // this.cherryInstancePreviewer.switchModel('editOnly');
        }
      } catch (error) {
        console.error('Error fetching article:', error);
        this.error = true;
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

cherry单例

```javascript
import 'cherry-markdown/src/sass/themes/diy.scss'
import Cherry from "cherry-markdown";
import 'cherry-markdown/dist/cherry-markdown.css';

let cherryInstance = null; // 单例实例

export function getCherryInstance() {
    if (!cherryInstance) {
        cherryInstance = new Cherry({
            id: "markdown-container",
            value: `# 公式测试`,
            externals: {
              echarts: window.echarts,
              katex: window.katex || null,
              MathJax: window.MathJax,
            },
            nameSpace: 'cherry',
            themeSettings: {
                // 主题列表,用于切换主题
                themeList: [
                    {className: 'div', label: 'div'},
                    {className: 'default', label: 'default'},
                    {className: 'dark', label: 'dark'},
                ],
                // 目前应用的主题
                mainTheme: 'div',
                // 目前应用的代码块主题
                codeBlockTheme: 'default',
            },
            engine: {
                syntax: {
                    mathBlock: {
                        plugins: true, // 默认加载插件
                        engine: 'MathJax', // katex或MathJax
                        src: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js', 
                    },
                    inlineMath: {
                        engine: 'MathJax', // katex或MathJax
                        src: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"
                    },
                    header: {
                        anchorStyle: "autonumber",
                        strict: false,
                    },
                },
            },
            toolbars: {
                toolbar: ['bold', 'italic', 'strikethrough', '|',
                    'color', 'ruby', '|',
                    'list', 'panel', 'detail', '|',
                    'theme', {insert: ['image', 'audio', 'video', 'link', 'code', 'formula', 'toc', 'table', 'drawIo']},
                    'togglePreview', 'switchModel',
                ],
            },
        });
    }
    return cherryInstance;
}

@sunsonliu
Copy link
Collaborator

哦哦,是因为Cherry对象无法被代理,可以看这个issue哈:#381 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working pending-no-confirmed The direction of this issue has not yet been determined.
Projects
None yet
Development

No branches or pull requests

2 participants