Skip to content

Commit

Permalink
feat: 支持 h1 到 h6 的剪藏
Browse files Browse the repository at this point in the history
  • Loading branch information
vagusX committed Jul 25, 2023
1 parent 895bd97 commit 5539e96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/components/editor/deserialize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { jsx } from 'slate-hyperscript';

function nodeNameToHeadingJsx(nodeName: string, children: any[]) {
return {
H1: jsx('element', { type: 'heading-one' }, children),
H2: jsx('element', { type: 'heading-two' }, children),
H3: jsx('element', { type: 'heading-three' }, children),
H4: jsx('element', { type: 'heading-four' }, children),
H5: jsx('element', { type: 'heading-five' }, children),
H6: jsx('element', { type: 'heading-six' }, children),
}[nodeName];
}

type SlateJSXElement = ReturnType<typeof jsx>;
const deserialize = (el: Node): SlateJSXElement | string | null => {
// 递归到 children 时直接返回 textContent string 格式
Expand Down Expand Up @@ -46,6 +57,13 @@ const deserialize = (el: Node): SlateJSXElement | string | null => {
},
children,
);
case 'H1':
case 'H2':
case 'H3':
case 'H4':
case 'H5':
case 'H6':
return nodeNameToHeadingJsx(htmlElement.nodeName, children);
default:
return jsx('element', { type: 'paragraph' }, [ htmlElement.textContent || '' ]);
}
Expand Down
5 changes: 3 additions & 2 deletions src/pages/sandbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { createRoot } from 'react-dom/client';
import 'antd/dist/antd.css';
import './index.module.less';

Expand All @@ -8,4 +8,5 @@ import App from './App';
const importAll = (r: any) => r.keys().forEach(r);
importAll(require.context('@/assets/icons', false, /\.png$/));

ReactDOM.render(<App />, document.getElementById('ReactApp'));
const root = createRoot(document.getElementById('ReactApp'));
root.render(<App />);

0 comments on commit 5539e96

Please sign in to comment.