Skip to content

Commit

Permalink
Shift+Space to start a new syllable
Browse files Browse the repository at this point in the history
When typing a polysyllabic loan word or VNY2K, press Shift+Space to begin a new syllable; subsequent input ignores the previous syllables for the purposes of spelling enforcement and diacritic placement.

Fixes #14.
  • Loading branch information
1ec5 committed Dec 28, 2015
1 parent d527b8d commit 001a23a
Showing 1 changed file with 51 additions and 11 deletions.
62 changes: 51 additions & 11 deletions content/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,11 @@ function applyKey(word, evt) {
}

/**
* Edits the word before the caret according to the given key press event.
*
* @param outer {object} The DOM node being edited.
* @param evt {Event} The key press event.
* @returns {object} The results of applying the event to the word.
* Returns the currently selected node in the given editor.
*/
function splice(outer, evt) {
let result = {};
let editor = getEditor(outer);
function getSelectedNode(editor) {
let sel = editor && editor.selection;
if (!sel || sel.rangeCount !== 1 || !sel.isCollapsed) return result;
if (!sel || sel.rangeCount !== 1 || !sel.isCollapsed) return null;
let node = sel.anchorNode;
// In Midas, clicking the end of the line takes the selection out of any
// text node into the document at large. (#69) When an element is selected,
Expand All @@ -334,7 +328,22 @@ function splice(outer, evt) {
node = node.childNodes[sel.anchorOffset - 1];
if (node.data) sel.collapse(node, node.data.length);
}
if (!sel.anchorOffset || !node.data) return result;
return node;
}

/**
* Edits the word before the caret according to the given key press event.
*
* @param outer {object} The DOM node being edited.
* @param evt {Event} The key press event.
* @returns {object} The results of applying the event to the word.
*/
function splice(outer, evt) {
let result = {};
let editor = getEditor(outer);
let sel = editor && editor.selection;
let node = getSelectedNode(editor);
if (!node || !sel.anchorOffset || !node.data) return result;

let word = lastWordInString(node.substringData(0, sel.anchorOffset));
result = word && applyKey(word, evt);
Expand Down Expand Up @@ -380,6 +389,24 @@ function splice(outer, evt) {
return result;
}

/**
* Splits the currently selected text node at the caret position so that
* subsequent input is treated as a new word.
*
* @param outer {object} The DOM node being edited.
* @param evt {Event} The key press event.
*/
function createWordSegment(outer, evt) {
let win = (outer.ownerDocument && outer.ownerDocument.defaultView) || outer;
let editor = getEditor(outer);
let node = getSelectedNode(editor);
if (node instanceof win.Text) {
let sel = editor.selection;
let newNode = node.splitText(sel.anchorOffset);
sel.collapse(newNode, 0);
}
}

/**
* Fires a fake onInput event from the given element. If preventDefault() is
* called on the onKeyPress event, most textboxes will not respond appropriately
Expand Down Expand Up @@ -455,6 +482,13 @@ function ifMoz(evt) {
if (findIgnore(elt)) return false;
if (win.frameElement && findIgnore(win.frameElement)) return false;

if (evt.shiftKey && evt.which === evt.DOM_VK_SPACE) {
createWordSegment(win, evt);
evt.stopPropagation();
evt.preventDefault();
return false;
}

let result = splice(win, evt);
if (result.changed) {
evt.stopPropagation();
Expand Down Expand Up @@ -483,6 +517,12 @@ function handleKeyPress(evt) {
AVIMConfig.exclude.indexOf("e-mail") < 0));
if (!isHTML || elt.selectionStart !== elt.selectionEnd) return false;

if (evt.shiftKey && evt.which === evt.DOM_VK_SPACE) {
createWordSegment(elt, evt);
evt.preventDefault();
return false;
}

let result = splice(elt, evt);
if (result.changed) {
evt.preventDefault();
Expand Down Expand Up @@ -756,7 +796,7 @@ function checkCode(evt) {
(code < evt.DOM_VK_INSERT &&
code !== evt.DOM_VK_BACK_SPACE &&
code !== evt.DOM_VK_PRINT &&
/* code !== evt.DOM_VK_SPACE && */
code !== evt.DOM_VK_SPACE &&
code !== evt.DOM_VK_RIGHT && code !== evt.DOM_VK_DOWN &&
code !== evt.DOM_VK_EXECUTE) ||
code === evt.DOM_VK_SCROLL_LOCK;
Expand Down

0 comments on commit 001a23a

Please sign in to comment.