mirror of https://github.com/fantasticit/think.git
tiptap: fix set title
This commit is contained in:
parent
de824badee
commit
6abd21545c
|
@ -5,28 +5,35 @@ const renderer = new Renderer();
|
||||||
/**
|
/**
|
||||||
* 将 HTML 转换成 prosemirror node
|
* 将 HTML 转换成 prosemirror node
|
||||||
* @param body
|
* @param body
|
||||||
* @param forceATitle 是否需要一个标题
|
* @param needTitle 是否需要一个标题
|
||||||
* @param defaultTitle 优先作为文档标题,否则默认读取一个 heading 或者 paragraph 的文字内容
|
* @param defaultTitle 优先作为文档标题,否则默认读取一个 heading 或者 paragraph 的文字内容
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const htmlToPromsemirror = (body, forceATitle = false, defaultTitle = '') => {
|
export const htmlToPromsemirror = (body, needTitle = false, defaultTitle = '') => {
|
||||||
const json = renderer.render(body);
|
const json = renderer.render(body);
|
||||||
|
|
||||||
// 设置标题
|
// 设置标题
|
||||||
if (forceATitle) {
|
if (needTitle) {
|
||||||
const forceTitleNode = json.content.find((node) => {
|
if (json.content[0].type !== 'title') {
|
||||||
return node.type === 'heading' || node.type === 'paragraph';
|
const forceTitleNode = json.content.find((node) => {
|
||||||
});
|
return node.type === 'heading' || node.type === 'paragraph';
|
||||||
|
});
|
||||||
|
|
||||||
json.content.unshift({
|
json.content.unshift({
|
||||||
type: 'title',
|
type: 'title',
|
||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
type: 'text',
|
type: 'text',
|
||||||
text: defaultTitle || forceTitleNode?.content[0]?.text.slice(0, 50),
|
text: defaultTitle || forceTitleNode?.content[0]?.text.slice(0, 50) || '未命名标题',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (json.content[0].type === 'title') {
|
||||||
|
json.content[0].type = 'paragraph';
|
||||||
|
json.content[0].attrs = {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodes = json.content;
|
const nodes = json.content;
|
||||||
|
@ -48,14 +55,5 @@ export const htmlToPromsemirror = (body, forceATitle = false, defaultTitle = '')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// trailing node
|
|
||||||
result.content.push({
|
|
||||||
type: 'paragraph',
|
|
||||||
attrs: {
|
|
||||||
indent: 0,
|
|
||||||
textAlign: 'left',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue