-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Editor#onTextInput
to handle text or regex-match input
Deprecate `Editor#registerTextExpansion` in favor of `onTextInput` Fixes #367
- Loading branch information
Showing
16 changed files
with
552 additions
and
292 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
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
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,24 @@ | ||
// from lodash | ||
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g, | ||
reHasRegExpChar = new RegExp(reRegExpChar.source); | ||
|
||
// from lodash | ||
function escapeForRegex(string) { | ||
return (string && reHasRegExpChar.test(string)) ? string.replace(reRegExpChar, '\\$&') : string; | ||
} | ||
|
||
export function convertExpansiontoHandler(expansion) { | ||
let { run: originalRun, text, trigger } = expansion; | ||
if (!!trigger) { | ||
text = text + String.fromCharCode(trigger); | ||
} | ||
let match = new RegExp('^' + escapeForRegex(text) + '$'); | ||
let run = (editor, ...args) => { | ||
let { range: { head } } = editor; | ||
if (head.isTail()) { | ||
originalRun(editor, ...args); | ||
} | ||
}; | ||
|
||
return { match, run }; | ||
} |
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
import { endsWith } from 'mobiledoc-kit/utils/string-utils'; | ||
import assert from 'mobiledoc-kit/utils/assert'; | ||
|
||
class TextInputHandler { | ||
constructor(editor) { | ||
this.editor = editor; | ||
this._handlers = []; | ||
} | ||
|
||
register(handler) { | ||
assert(`Input Handler is not valid`, this._validateHandler(handler)); | ||
this._handlers.push(handler); | ||
} | ||
|
||
handle(string) { | ||
let { editor } = this; | ||
editor.insertText(string); | ||
|
||
let matchedHandler = this._findHandler(); | ||
if (matchedHandler) { | ||
let [ handler, matches ] = matchedHandler; | ||
handler.run(editor, matches); | ||
} | ||
} | ||
|
||
_findHandler() { | ||
let { editor: { range: { head, head: { section } } } } = this; | ||
let preText = section.textUntil(head); | ||
|
||
for (let i=0; i < this._handlers.length; i++) { | ||
let handler = this._handlers[i]; | ||
let {text, match} = handler; | ||
|
||
if (text && endsWith(preText, text)) { | ||
return [handler, [text]]; | ||
} else if (match && match.test(preText)) { | ||
return [handler, match.exec(preText)]; | ||
} | ||
} | ||
} | ||
|
||
_validateHandler(handler) { | ||
return !!handler.run && // has `run` | ||
(!!handler.text || !!handler.match) && // and `text` or `match` | ||
!(!!handler.text && !!handler.match); // not both `text` and `match` | ||
} | ||
|
||
destroy() { | ||
this._handlers = []; | ||
} | ||
} | ||
|
||
export default TextInputHandler; |
Oops, something went wrong.