Skip to content

Commit

Permalink
move commands into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
songziming committed Mar 10, 2024
1 parent 59f7db4 commit cb5d0f9
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 956 deletions.
769 changes: 13 additions & 756 deletions apps/notes/package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions apps/notes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"recoil": "^0.7.7",
"slate": "^0.102.0",
"slate-history": "^0.100.0",
"slate-react": "^0.102.0"
},
"scripts": {
Expand Down
39 changes: 0 additions & 39 deletions apps/notes/src/CustomEditor.js

This file was deleted.

10 changes: 8 additions & 2 deletions apps/notes/src/Editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
left: 10px;
bottom: 10px;
right: 10px;
padding: 0 10px;
/* padding: 0 10px; */
border: 1px dashed #777;
border-radius: 4px;
}


.para {
.editable {
height: 100%;
overflow-y: scroll;
}


.block {
background-color: #eee;
overflow: auto; /* 将子元素的 margin 包含进来 */
max-width: 960px;
Expand Down
76 changes: 76 additions & 0 deletions apps/notes/src/SlateCommands.js
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,
};

0 comments on commit cb5d0f9

Please sign in to comment.