Commit 5b8e6efe authored by Яков's avatar Яков
Browse files

update fix issue

parent fd07d727
{
"name": "react-ag-qeditor",
"version": "1.1.58",
"version": "1.1.59",
"description": "WYSIWYG html editor",
"author": "atma",
"license": "MIT",
......
......@@ -51,7 +51,7 @@ import IframeModal from './modals/IframeModal'
import IframeCustomModal from './modals/IframeCustomModal'
import { isMobile } from 'react-device-detect'
import { ExportPdf } from './extensions/ExportPdf'
import { mergeAttributes } from "@tiptap/core";
import { mergeAttributes, Extension } from "@tiptap/core";
import Upload from "rc-upload";
import { NodeSelection, TextSelection } from 'prosemirror-state'
......@@ -70,6 +70,76 @@ import { NodeSelection, TextSelection } from 'prosemirror-state'
// })
const {TextArea} = Input;
// Word-by-word navigation (Word-like: Ctrl+Arrow)
function _wordFwd(text, pos) {
let i = pos
while (i < text.length && /\s/.test(text[i])) i++
while (i < text.length && /\S/.test(text[i])) i++
return i
}
function _wordBwd(text, pos) {
let i = pos
while (i > 0 && /\s/.test(text[i - 1])) i--
while (i > 0 && /\S/.test(text[i - 1])) i--
return i
}
function _moveByWord(editor, forward, extend) {
const { state } = editor
const { selection, doc } = state
const { $anchor, $head } = selection
const $cursor = extend ? $head : (forward ? selection.$to : selection.$from)
const pos = $cursor.pos
const blockStart = $cursor.start()
const blockEnd = $cursor.end()
const blockText = doc.textBetween(blockStart, blockEnd, '', '')
const relPos = pos - blockStart
let newPos
if (forward) {
const nr = _wordFwd(blockText, relPos)
if (nr === relPos && blockEnd + 1 <= doc.content.size) {
// no movement possible in this block — jump into next block
try {
const $next = doc.resolve(blockEnd + 1)
newPos = $next.start()
} catch { newPos = blockEnd }
} else {
newPos = blockStart + nr
}
} else {
const nr = _wordBwd(blockText, relPos)
if (nr === relPos && blockStart - 1 > 0) {
// no movement possible in this block — jump to end of previous block
try {
const $prev = doc.resolve(blockStart - 1)
newPos = $prev.end()
} catch { newPos = Math.max(0, blockStart - 1) }
} else {
newPos = blockStart + nr
}
}
newPos = Math.max(0, Math.min(newPos, doc.content.size))
const anchorPos = extend ? $anchor.pos : newPos
try {
const sel = TextSelection.create(doc, anchorPos, newPos)
editor.view.dispatch(state.tr.setSelection(sel).scrollIntoView())
} catch { /* invalid position */ }
return true
}
const WordNavigation = Extension.create({
name: 'wordNavigation',
addKeyboardShortcuts() {
return {
'Ctrl-ArrowRight': () => _moveByWord(this.editor, true, false),
'Ctrl-ArrowLeft': () => _moveByWord(this.editor, false, false),
'Ctrl-Shift-ArrowRight': () => _moveByWord(this.editor, true, true),
'Ctrl-Shift-ArrowLeft': () => _moveByWord(this.editor, false, true),
}
}
})
const initialBubbleItems = [
'bold',
'italic',
......@@ -740,6 +810,7 @@ const QEditor = ({
Subscript,
ToggleBlock,
InteractiveImage,
WordNavigation,
DragAndDrop.configure({
uploadUrl: uploadOptions.url,
allowedFileTypes: [
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment