red-axe / am-editor

A rich text editor that supports collaborative editing and allows for the free use of front-end common libraries such as React and Vue to extend and define plugins.
https://editor.aomao.com
MIT License
936 stars 197 forks source link

由于后端返回的数据有两种格式一种是html,一种是mardown语法,有没有办法处理下数据 #246

Closed chunyangyang closed 2 years ago

chunyangyang commented 2 years ago

1.项目有些文档数据是存储的mardown语法,导致页面渲染需要兼容两种语法 现在协同如果是mardown语法会渲染不了,求帮助 2.使用协同没有光标是什么原因? 示例图

big-camel commented 2 years ago

当前信息我也无法判断

chunyangyang commented 2 years ago

后端数据:

### 吹过的风更改 #
dfsdf是 ```javascript dfdsf dsfdsf npm run dev ``` / > 第三方 djkfdksjfh
dfsdfdscasE
是的 **x**Space
`**SS杀杀杀**`
是的
是的 :::info ::: DFD :::success ::: | ddd |
| | | --- | --- | --- | | | | | | | | | # 发发生的f DFDS
[]() ---

目前兼容代码 image

2.光标问题,我是用的example/react的代码,去掉了评论相关

chunyangyang commented 2 years ago


# <br />
dfsdf是

dfdsf
dsfdsf 
npm run dev

/
&gt; 第三方

djkfdksjfh<br />dfsdfdscasE<br />是的

**x**Space<br />`**SS杀杀杀**`<br />是的<br />是的
:::info

:::
DFD 

:::success
:::
| ddd | <br /> |  |
| --- | --- | --- |
|  |  |  |
|  |  |  |

# 发发生的f

DFDS<br />[]() 

---</p>
chunyangyang commented 2 years ago

有办法处理吗

big-camel commented 2 years ago

要不你给个最小复现吧

wwhwwhwwh commented 2 years ago

engine\src\change\index.ts setMarkdown markdown.enable(['paragraph', 'html_inline', 'newline']) 有些语法没有开启转换 image ,table,header

big-camel commented 2 years ago

这些都是在插件里面启用的 from 阿里邮箱 iPhone------------------------------------------------------------------ @.> 日 期:2022年09月06日 12:20:39 @.> @.>; @.> 主 题:Re: [red-axe/am-editor] 由于后端返回的数据有两种格式一种是html,一种是mardown语法,有没有办法处理下数据 (Issue #246)

engine\src\change\index.ts setMarkdown markdown.enable(['paragraph', 'html_inline', 'newline']) 有些语法没有开启转换 image ,table,header — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

wwhwwhwwh commented 2 years ago

是需要调用这个API启用吧

engine.on('markdown-it', (markdown) => {
    markdown.enable('markdown-it 插件名称');
    markdown.use(markdown - it插件);
});
big-camel commented 2 years ago

插件内是调用了的

wwhwwhwwh commented 2 years ago

setMarkdown 之后,需要在内容最后按下 回车键 才会渲染 MarkdownShow

big-camel commented 2 years ago

更新一下到最新版本再试试

wwhwwhwwh commented 2 years ago

最新版测试没问题

chunyangyang commented 2 years ago

复现代码不怎么好整理,我看了你的源码,是在协同的时候,值是在这个路径下 packages\engine\src\ot\index.ts的syncValue方法设置的

syncValue(defaultValue?: string) {
        const { doc, engine } = this;
        if (!doc) return;
        // 除了div 和 selection-data 外 还必须有其它节点
        if (doc.type && Array.isArray(doc.data) && doc.data.length > 2) {
            // 远端有数据就设置数据到当前编辑器
            this.engine.setJsonValue(doc.data);
            return;
        }
        // 如果有设置默认值,就设置编辑器的值
        if (defaultValue) engine.setValue(defaultValue);
        // 没有数据,就把当前编辑器值提交
        doc.on('create', () => {
            const data = toJSON0(engine.container);
            (doc as Doc).submitOp(
                [
                    {
                        p: [],
                        oi: data,
                    },
                ],
                {
                    source: this.clientId,
                },
            );
        });
    }

之前我没有做协同是自己做了个判断,代码如下:

// 非协同编辑,设置编辑器值,异步渲染后回调
        // console.log(' inflateValue', inflateValue);
        // console.log(' inflateValue.substring(0, 1) == "<"==>', inflateValue.indexOf('data-id=') != -1);
        if (inflateValue.indexOf('data-id=') != -1) {
          engine.current.setValue(inflateValue, (count) => {
            console.log("setValue loaded:", count);
            if (onLoad) onLoad(engine.current!);
            return setLoading(false);
          });
        } else {
          engine.current.setMarkdown(inflateValue, (count) => {
            console.log("setValue loaded:", count);
            if (onLoad) onLoad(engine.current!);
            return setLoading(false);
          });
        }`

现在的问题就是,我想在协同的时候也要实现相同功能!!!

big-camel commented 2 years ago

只能这样看看能不能先转换成 value 了

import {  convertMarkdown, createMarkdownIt, Parser } from '@aomao/engine'
const markdown = createMarkdownIt(engine, 'zero');
markdown.enable(['paragraph', 'html_inline', 'newline']);
const tokens = markdown.parse(text, {});
if (tokens.length === 0) return;
const html = convertMarkdown(this.engine, markdown, tokens);

const parser = new Parser(value, engine)
const value = parser.toValue(engine.schema, engine.conversion)
chunyangyang commented 2 years ago

只能这样看看能不能先转换成 value 了 协同赋值是你这边赋值是你里面写死了这样赋值,我要怎么加你这段代码呢?


// 除了div 和 selection-data 外 还必须有其它节点
if (doc.type && Array.isArray(doc.data) && doc.data.length > 2) {
// 远端有数据就设置数据到当前编辑器
this.engine.setJsonValue(doc.data);
return;
}```
big-camel commented 2 years ago

你获取到后端的md数据后,用上面代码转成 value 格式。然后 https://github.com/red-axe/am-editor/blob/master/examples/react/components/editor/index.tsx#L194 这里把这个 value 传过去,ot 那边如果没有值就会使用这个值设置到 ot 当中,ot 就会更新到编辑器上

chunyangyang commented 2 years ago

你获取到后端的md数据后,用上面代码转成 value 格式。然后 https://github.com/red-axe/am-editor/blob/master/examples/react/components/editor/index.tsx#L194 这里把这个 value 传过去,ot 那边如果没有值就会使用这个值设置到 ot 当中,ot 就会更新到编辑器上

按照上面的设置还是不行,可以麻烦看下原因吗?

我的代码如下:

//inflateValue是后端返回的数据
   const markdown = createMarkdownIt(engine.current, 'zero');
      markdown.enable(['paragraph', 'html_inline', 'newline']);
      const tokens = markdown.parse(inflateValue, {});
      if (tokens.length === 0) return;
      const html = convertMarkdown(engine.current, markdown, tokens);

      const parser = new Parser(inflateValue, engine.current)
      inflateValue = parser.toValue(engine.current.schema, engine.current.conversion)
      console.log('html==》', html);
      console.log('inflateValue==》', inflateValue);

返回的结果如下:

html==》 <p><p data-id="p838747a-j2QFl1fj">### 这是一段代码</p><p data-id="p838747a-2BiZZIhQ">​<code>​json {   &amp;quot;appName&amp;quot;: &amp;quot;tech-main&amp;quot;,   &amp;quot;origin&amp;quot;: &amp;quot;/api/usm/&amp;quot;,   &amp;quot;target&amp;quot;: &amp;quot;<a href="http://10.219.12.51:905/microservices-usm-spring-cloud-starter&amp;quot;">http://10.219.12.51:905/microservices-usm-spring-cloud-starter&amp;quot;</a>,   &amp;quot;rewriteFrom&amp;quot;: &amp;quot;^/api/usm/&amp;quot;,   &amp;quot;rewriteTo&amp;quot;: &amp;quot;/Api/usm/&amp;quot;,     &amp;quot;description&amp;quot;: &amp;quot;&amp;quot; } ​</code>​</p><p data-id="p838747a-nf2JfKn3">复合弓得到的单独单独的4500</p><p data-id="p838747a-US2pQWT4">的</p><p data-id="p838747a-KJKYmjkU">你是搜索</p><p data-id="p838747a-ITIcdSDS"><cursor /><br /></p><p data-id="p838747a-1j6EjRkc"><br /></p></p>

 inflateValue==》 <p data-id="p838747a-j2QFl1fj">### 这是一段代码</p><p data-id="p838747a-2BiZZIhQ">​<code>​json {   &quot;appName&quot;: &quot;tech-main&quot;,   &quot;origin&quot;: &quot;/api/usm/&quot;,   &quot;target&quot;: &quot;http://10.219.12.51:905/microservices-usm-spring-cloud-starter&quot;,   &quot;rewriteFrom&quot;: &quot;^/api/usm/&quot;,   &quot;rewriteTo&quot;: &quot;/Api/usm/&quot;,     &quot;description&quot;: &quot;&quot; } ​</code>​</p><p data-id="p838747a-nf2JfKn3">复合弓得到的单独单独的4500</p><p data-id="p838747a-US2pQWT4">的</p><p data-id="p838747a-KJKYmjkU">你是搜索</p><p data-id="p838747a-ITIcdSDS"><span data-element="cursor"></span><br /></p><p data-id="p838747a-1j6EjRkc"><br /></p>

页面效果如果:

### 这是一段代码
​​json {   "appName": "tech-main",   "origin": "/api/usm/",   "target": "http://10.219.12.51:905/microservices-usm-spring-cloud-starter",   "rewriteFrom": "^/api/usm/",   "rewriteTo": "/Api/usm/",     "description": "" } ​​
复合弓得到的单独单独的4500
的
你是搜索
wwhwwhwwh commented 2 years ago

还有这种 code不支持

code:`Ctrl+F`
big-camel commented 2 years ago

你获取到后端的md数据后,用上面代码转成 value 格式。然后 https://github.com/red-axe/am-editor/blob/master/examples/react/components/editor/index.tsx#L194 这里把这个 value 传过去,ot 那边如果没有值就会使用这个值设置到 ot 当中,ot 就会更新到编辑器上

按照上面的设置还是不行,可以麻烦看下原因吗?

我的代码如下:

//inflateValue是后端返回的数据
   const markdown = createMarkdownIt(engine.current, 'zero');
      markdown.enable(['paragraph', 'html_inline', 'newline']);
      const tokens = markdown.parse(inflateValue, {});
      if (tokens.length === 0) return;
      const html = convertMarkdown(engine.current, markdown, tokens);

      const parser = new Parser(inflateValue, engine.current)
      inflateValue = parser.toValue(engine.current.schema, engine.current.conversion)
      console.log('html==》', html);
      console.log('inflateValue==》', inflateValue);

返回的结果如下:

html==》 <p><p data-id="p838747a-j2QFl1fj">### 这是一段代码</p><p data-id="p838747a-2BiZZIhQ">​<code>​json {   &amp;quot;appName&amp;quot;: &amp;quot;tech-main&amp;quot;,   &amp;quot;origin&amp;quot;: &amp;quot;/api/usm/&amp;quot;,   &amp;quot;target&amp;quot;: &amp;quot;<a href="http://10.219.12.51:905/microservices-usm-spring-cloud-starter&amp;quot;">http://10.219.12.51:905/microservices-usm-spring-cloud-starter&amp;quot;</a>,   &amp;quot;rewriteFrom&amp;quot;: &amp;quot;^/api/usm/&amp;quot;,   &amp;quot;rewriteTo&amp;quot;: &amp;quot;/Api/usm/&amp;quot;,     &amp;quot;description&amp;quot;: &amp;quot;&amp;quot; } ​</code>​</p><p data-id="p838747a-nf2JfKn3">复合弓得到的单独单独的4500</p><p data-id="p838747a-US2pQWT4">的</p><p data-id="p838747a-KJKYmjkU">你是搜索</p><p data-id="p838747a-ITIcdSDS"><cursor /><br /></p><p data-id="p838747a-1j6EjRkc"><br /></p></p>

 inflateValue==》 <p data-id="p838747a-j2QFl1fj">### 这是一段代码</p><p data-id="p838747a-2BiZZIhQ">​<code>​json {   &quot;appName&quot;: &quot;tech-main&quot;,   &quot;origin&quot;: &quot;/api/usm/&quot;,   &quot;target&quot;: &quot;http://10.219.12.51:905/microservices-usm-spring-cloud-starter&quot;,   &quot;rewriteFrom&quot;: &quot;^/api/usm/&quot;,   &quot;rewriteTo&quot;: &quot;/Api/usm/&quot;,     &quot;description&quot;: &quot;&quot; } ​</code>​</p><p data-id="p838747a-nf2JfKn3">复合弓得到的单独单独的4500</p><p data-id="p838747a-US2pQWT4">的</p><p data-id="p838747a-KJKYmjkU">你是搜索</p><p data-id="p838747a-ITIcdSDS"><span data-element="cursor"></span><br /></p><p data-id="p838747a-1j6EjRkc"><br /></p>

页面效果如果:

### 这是一段代码
​​json {   "appName": "tech-main",   "origin": "/api/usm/",   "target": "http://10.219.12.51:905/microservices-usm-spring-cloud-starter",   "rewriteFrom": "^/api/usm/",   "rewriteTo": "/Api/usm/",     "description": "" } ​​
复合弓得到的单独单独的4500
的
你是搜索

更新到最新版本了吗?

chunyangyang commented 2 years ago

是2.9.37

big-camel commented 2 years ago

image

import { createMarkdownIt, convertMarkdown, Parser } from '@aomao/engine';

let inflateValue = '### This is Heading3';
const markdown = createMarkdownIt(engine, 'zero');
markdown.enable(['paragraph', 'html_inline', 'newline']);
const tokens = markdown.parse(inflateValue, {});
if (tokens.length === 0) return;
const html = convertMarkdown(engine, markdown, tokens);

const parser = new Parser(inflateValue, engine)
inflateValue = parser.toValue(engine.schema, engine.conversion)
console.log('html==》', html);
console.log('inflateValue==》', inflateValue);