Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode/emoji offset on remove and move ops #2680

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 2 additions & 15 deletions packages/slate-react/src/plugins/dom/after.js
Expand Up @@ -3,7 +3,6 @@ import Debug from 'debug'
import Hotkeys from 'slate-hotkeys'
import Plain from 'slate-plain-serializer'
import getWindow from 'get-window'
import { TextUtils } from 'slate'
import { IS_IOS, IS_IE, IS_EDGE } from 'slate-dev-environment'

import cloneFragment from '../../utils/clone-fragment'
Expand Down Expand Up @@ -528,13 +527,7 @@ function AfterPlugin(options = {}) {
return editor.moveToStart()
}

const { start } = selection
const startBlock = document.getClosestBlock(start.key)
const offset = startBlock.getOffset(start.key)
const o = offset + start.offset
const { text } = startBlock
const n = TextUtils.getCharOffsetBackward(text, o)
return editor.moveBackward(n)
return editor.moveBackward()
}

if (Hotkeys.isMoveForward(event)) {
Expand All @@ -544,13 +537,7 @@ function AfterPlugin(options = {}) {
return editor.moveToEnd()
}

const { start } = selection
const startBlock = document.getClosestBlock(start.key)
const offset = startBlock.getOffset(start.key)
const o = offset + start.offset
const { text } = startBlock
const n = TextUtils.getCharOffsetForward(text, o)
return editor.moveForward(n)
return editor.moveForward()
}

if (Hotkeys.isMoveWordBackward(event)) {
Expand Down
16 changes: 8 additions & 8 deletions packages/slate/src/commands/at-range.js
Expand Up @@ -410,11 +410,11 @@ Commands.deleteCharBackwardAtRange = (editor, range) => {
const { document } = value
const { start } = range
const startBlock = document.getClosestBlock(start.path)
const offset = startBlock.getOffset(start.key)
const o = offset + start.offset
const o = startBlock.getOffset(start.key)
const offset = o + start.offset
const { text } = startBlock
const n = TextUtils.getCharOffsetBackward(text, o)
editor.deleteBackwardAtRange(range, n)
const charsOffset = TextUtils.getCharsOffsetBackward(text, offset, 1)
editor.deleteBackwardAtRange(range, charsOffset)
}

/**
Expand All @@ -434,11 +434,11 @@ Commands.deleteCharForwardAtRange = (editor, range) => {
const { document } = value
const { start } = range
const startBlock = document.getClosestBlock(start.path)
const offset = startBlock.getOffset(start.key)
const o = offset + start.offset
const o = startBlock.getOffset(start.key)
const offset = o + start.offset
const { text } = startBlock
const n = TextUtils.getCharOffsetForward(text, o)
editor.deleteForwardAtRange(range, n)
const charsOffset = TextUtils.getCharsOffsetForward(text, offset, 1)
editor.deleteForwardAtRange(range, charsOffset)
}

/**
Expand Down
28 changes: 24 additions & 4 deletions packages/slate/src/commands/on-selection.js
Expand Up @@ -131,8 +131,18 @@ Commands.moveAnchorToStartOfText = editor => {
editor.command(pointEdgeObject, 'anchor', 'start', 'text')
}

Commands.moveBackward = (editor, ...args) => {
editor.moveAnchorBackward(...args).moveFocusBackward(...args)
Commands.moveBackward = (editor, chars = 1) => {
if (chars === 0) return

const { value } = editor
const { document, selection } = value
const { start } = selection
const startBlock = document.getClosestBlock(start.key)
const o = startBlock.getOffset(start.key)
const offset = o + start.offset
const { text } = startBlock
const charsOffset = TextUtils.getCharsOffsetBackward(text, offset, chars)
editor.moveAnchorBackward(charsOffset).moveFocusBackward(charsOffset)
}

Commands.moveWordBackward = (editor, ...args) => {
Expand Down Expand Up @@ -355,8 +365,18 @@ Commands.moveFocusToStartOfText = editor => {
editor.command(pointEdgeObject, 'focus', 'start', 'text')
}

Commands.moveForward = (editor, ...args) => {
editor.moveAnchorForward(...args).moveFocusForward(...args)
Commands.moveForward = (editor, chars = 1) => {
if (chars === 0) return

const { value } = editor
const { document, selection } = value
const { start } = selection
const startBlock = document.getClosestBlock(start.path)
const o = startBlock.getOffset(start.key)
const offset = o + start.offset
const { text } = startBlock
const charsOffset = TextUtils.getCharsOffsetForward(text, offset, chars)
editor.moveAnchorForward(charsOffset).moveFocusForward(charsOffset)
}

Commands.moveWordForward = (editor, ...args) => {
Expand Down