Skip to content

Commit

Permalink
Merge branch 'features/table-insert-break'
Browse files Browse the repository at this point in the history
  • Loading branch information
cycleccc committed Oct 18, 2024
2 parents c18aa53 + 8211031 commit 9fb7be8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/table-module/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('TableModule module', () => {

newEditor.insertBreak()

expect(mockFn).toBeCalledWith('\n')
expect(mockFn).toBeCalledWith('\n\r')
})

test('use withTable plugin when insertData should insertText to cell', () => {
Expand Down
17 changes: 11 additions & 6 deletions packages/table-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
NodeEntry,
Path,
Point,
Selection,
Text,
Transforms,
Selection,
} from 'slate'

import { withSelection } from './with-selection'
Expand Down Expand Up @@ -51,6 +51,7 @@ function deleteHandler(newEditor: IDomEditor): boolean {
*/
function isHalfBreak(newEditor: IDomEditor, location: Point): boolean {
const offset = location.offset

if (offset === 0) { return false }
const node = Editor.node(newEditor, location)

Expand Down Expand Up @@ -259,29 +260,33 @@ function withTable<T extends IDomEditor>(editor: T): T {
}

// 重写区域选中的删除,修正可能半选的换行符
newEditor.deleteFragment = (unit) => {
newEditor.deleteFragment = unit => {
const { selection } = newEditor
if (!selection) return

if (!selection) { return }
let hasChange = false
const newSelection: Selection = {
anchor: selection.anchor,
focus: selection.focus
focus: selection.focus,
}
// 是否是从左到右的选区
const isLeftToRight = Point.isBefore(newSelection.anchor, newSelection.focus)

if (isHalfBreak(newEditor, selection.anchor)) {
const nv = Editor[isLeftToRight ? 'before' : 'after'](newEditor, selection.anchor)

if (nv) {
newSelection.anchor = nv
}
hasChange = true;
hasChange = true
}
if (isHalfBreak(newEditor, selection.focus)) {
const nv = Editor[isLeftToRight ? 'after' : 'before'](newEditor, selection.focus)

if (nv) {
newSelection.focus = nv
}
hasChange = true;
hasChange = true
}
if (hasChange) {
Transforms.setSelection(newEditor, newSelection)
Expand Down

0 comments on commit 9fb7be8

Please sign in to comment.