Skip to content

Commit 88df36c

Browse files
committed
feat: ✨ Supports break line
1 parent 0f97a4d commit 88df36c

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

README-zh_CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
## 快捷键
1414
- `Tab`: 移动光标到下一个单元格
1515
- `Shift + Tab`: 移动光标到上一个单元格
16+
- `Shift + Enter`: 在当前单元格换行
1617

1718
## 开发
1819
```shell

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ English | [简体中文](./README-zh_CN.md)
77

88
reference[https://codesandbox.io/s/yt8jc](https://codesandbox.io/s/yt8jc)
99

10-
[Roadmap](https://www.notion.so/haydenut/logseq-plugin-markdown-table-a95f18109a894b9a803b613ce05ac697)
11-
1210
## Read Before Use
1311
- **Multiple tables need to be separated by blank lines, otherwise they will be recognized as one table**
1412

@@ -18,6 +16,7 @@ reference[https://codesandbox.io/s/yt8jc](https://codesandbox.io/s/yt8jc)
1816
## Shortcuts
1917
- `Tab`: Move cursor to the next cell
2018
- `Shift + Tab`: Move cursor to the previous cell
19+
- `Shift + Enter`: Break line in the current cell
2120

2221
## Development
2322
```shell

src/components/TableEditor.jsx

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ const TableEditor = ({ content = DEFAULT_TABLE, className = '' }, ref) => {
8686
const tableUtil = useMemo(() => new TableUtil(editor), [editor])
8787
const renderElement = useCallback(props => <Element {...props} />, [])
8888

89+
const onKeyDown = event => {
90+
// https://docs.slatejs.org/libraries/slate-react#editable
91+
if (event.key === 'Enter' && event.shiftKey) {
92+
editor.insertText('\n')
93+
return false
94+
}
95+
}
96+
8997
useImperativeHandle(
9098
ref,
9199
() => ({
@@ -114,6 +122,7 @@ const TableEditor = ({ content = DEFAULT_TABLE, className = '' }, ref) => {
114122
<Editable
115123
placeholder='Write something'
116124
renderElement={renderElement}
125+
onKeyDown={onKeyDown}
117126
/>
118127
</Slate>
119128
</div>

src/utils/testExample.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export const tableWithTextBeforeAndAfter = 'foo\n`yarn install`\n|title1|title2|
1010

1111
export const multipleTables = 'foo\n`yarn install`\n|title1|title2|\n|--|--|\n|content1|content2|\n|**bold**|\nother\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\ntest text'
1212

13-
export const longTables = 'foo\n`yarn install`\n|title1|title2|\n|--|--|\n|content1|content2|\n\n**bold**\nother\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\n|contentB|contentB|\n|contentB|contentB|\n|contentB|contentB|'
13+
export const longTables = 'foo\n`yarn install`\n|title1[:br]new line|title2|\n|--|--|\n|content1|content2|\n\n**bold**\nother\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\n|contentB|contentB|\n|contentB|contentB|\n|contentB|contentB|'

src/utils/util.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { DEFAULT_TABLE } from './contants'
22

33
export const stringToSlateValue = (str = '') => {
44
str = str || DEFAULT_TABLE
5-
const _arr = str.trim().split('\n').filter(Boolean)
5+
// 将 [:br] 转为换行符
6+
const _arr = str.trim().split('\n').filter(Boolean).map(_str => _str.replaceAll('[:br]', '\n'))
67
const contentArr = [_arr[0]].concat(_arr.slice(2))
78
const res = contentArr.map(rowStr => {
89
const rowArr = rowStr.trim().split('|')
@@ -14,7 +15,8 @@ export const stringToSlateValue = (str = '') => {
1415
export const slateValueToString = (slateVal) => {
1516
let rowStrs = Array.from(slateVal.children, (row) => {
1617
const cells = Array.from(row.children, (cell) => {
17-
return cell.children[0].text
18+
// 将换行符替换为 [:br]
19+
return cell.children[0].text?.replaceAll('\n', '[:br]')
1820
}).join('|')
1921
return `|${cells}|`
2022
})

0 commit comments

Comments
 (0)