-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59f7db4
commit cb5d0f9
Showing
11 changed files
with
164 additions
and
956 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Transforms, Editor, Element } from 'slate'; | ||
|
||
|
||
// 各种编辑都是针对选中部分 | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// 对 Leaf 样式的改动 | ||
//------------------------------------------------------------------------------ | ||
|
||
const toggleBold = (editor) => { | ||
if (Editor.marks(editor)?.bold) { | ||
Editor.removeMark(editor, 'bold'); | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// 修改块类型 | ||
//------------------------------------------------------------------------------ | ||
|
||
// TODO 需要分清转换之前是什么类型,转换为什么类型,目标类型是否支持保留样式 | ||
|
||
// 将文本包一层 codeblock,原来的 block 变成 codeline,去掉样式 | ||
const toCodeBlock = (editor) => { | ||
Transforms.wrapNodes(editor, { type: 'codeblock', lang: 'py' }, { | ||
match: n => Element.isElement(n) && (n.type === 'codeline' || n.type === 'paragraph'), | ||
split: true, | ||
}); | ||
Transforms.setNodes(editor, { type: 'codeline' }, { | ||
match: n => Element.isElement(n) && (n.type === 'codeline' || n.type === 'paragraph') | ||
}); | ||
Transforms.setNodes(editor, { bold: false }, { | ||
match: n => !Element.isElement(n) | ||
}); | ||
} | ||
|
||
|
||
// TODO 需要识别当前段落的类型,再有针对性地提取文字,变为普通段落 | ||
const toParagraph = (editor) => { | ||
// 将 codeblock 拆开 | ||
Transforms.unwrapNodes(editor, { | ||
match: n => Element.isElement(n) && n.type === 'codeblock' | ||
}); | ||
// 里面的 codeline 换成普通段落 | ||
Transforms.setNodes(editor, | ||
{ type: 'paragraph' }, | ||
{ match: n => Element.isElement(n) && n.type === 'codeline' } | ||
); | ||
}; | ||
|
||
|
||
|
||
const toggleCode = (editor) => { | ||
const [match] = Editor.nodes(editor, { | ||
match: n => n.type === 'codeblock', | ||
}); | ||
|
||
if (!match) { | ||
toCodeBlock(editor); | ||
} else { | ||
toParagraph(editor); | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
|
||
|
||
export { | ||
toggleBold, | ||
toCodeBlock, toParagraph, toggleCode, | ||
}; |
Oops, something went wrong.