Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
602 additions
and
582 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Inline } from "slate"; | ||
|
||
import { getPreviousNode, isFirstCharOfNode } from "./queries"; | ||
|
||
export const focusPreviousNode = editor => { | ||
if (isFirstCharOfNode(editor)) { | ||
const previousNode = getPreviousNode(editor); | ||
if (previousNode && Inline.isInline(previousNode)) { | ||
editor.moveToEndOfNode(previousNode).normalize(document); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const isPrintableChar = event => event.key.length === 1; | ||
|
||
export const isCtrlOrCmd = event => event.ctrlKey || event.metaKey; | ||
|
||
export const isSpace = event => event.key === " "; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export const getSelection = editor => editor.value.fragment.text; | ||
|
||
export const getCurrentWordOffset = editor => | ||
editor.value.selection.focus.offset; | ||
|
||
export const isFirstCharOfNode = editor => getCurrentWordOffset(editor) === 0; | ||
|
||
export const getPreviousNode = editor => { | ||
const block = editor.value.focusBlock; | ||
const focusKey = editor.value.selection.focus.key; | ||
return block.getPreviousSibling(focusKey); | ||
}; | ||
|
||
export const getLastWord = editor => { | ||
const currentWordOffset = getCurrentWordOffset(editor); | ||
return getLastWordRec(editor, currentWordOffset); | ||
}; | ||
|
||
const getLastWordRec = (editor, maxIndex, counter = 0) => { | ||
// If first char of the input is found just select everything (single word) | ||
if (counter === maxIndex) { | ||
const selectedWord = getSelection(editor); | ||
editor.moveFocusForward(counter); | ||
return selectedWord; | ||
} | ||
|
||
// Move selection | ||
const selectedWord = getSelection(editor.moveFocusBackward(1)); | ||
|
||
// If we find a space it means previous chars are the last word ! | ||
if (selectedWord[0] === " ") { | ||
editor.moveFocusForward(counter + 1); // one more needed because space | ||
return selectedWord.substring(1); | ||
} | ||
|
||
return getLastWordRec(editor, maxIndex, counter + 1); | ||
}; |
Oops, something went wrong.