-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More editor related migration π§ββοΈπ²
- Loading branch information
Showing
9 changed files
with
175 additions
and
136 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 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,41 @@ | ||
import SelectionChangeObserver from '../editor/selection-change-observer' | ||
import Editor from './editor' | ||
import { PartialSelection } from '../utils/selection-utils' | ||
|
||
type SelectionManagerCallback = (curSelection: PartialSelection, prevSelection: PartialSelection) => void | ||
|
||
export default class SelectionManager { | ||
editor: Editor | ||
callback: SelectionManagerCallback | ||
started: boolean | ||
|
||
constructor(editor: Editor, callback: SelectionManagerCallback) { | ||
this.editor = editor | ||
this.callback = callback | ||
this.started = false | ||
} | ||
|
||
start() { | ||
if (this.started) { | ||
return | ||
} | ||
|
||
SelectionChangeObserver.addListener(this) | ||
this.started = true | ||
} | ||
|
||
stop() { | ||
this.started = false | ||
SelectionChangeObserver.removeListener(this) | ||
} | ||
|
||
destroy() { | ||
this.stop() | ||
} | ||
|
||
selectionDidChange(curSelection: PartialSelection, prevSelection: PartialSelection) { | ||
if (this.started) { | ||
this.callback(curSelection, prevSelection) | ||
} | ||
} | ||
} |
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,102 @@ | ||
import { endsWith } from '../utils/string-utils' | ||
import assert from '../utils/assert' | ||
import deprecate from '../utils/deprecate' | ||
import { ENTER } from '../utils/characters' | ||
import Editor from './editor' | ||
import Markerable from '../models/_markerable' | ||
import { Maybe } from '../utils/types' | ||
|
||
class TextInputHandler { | ||
editor: Editor | ||
_handlers: TextInputHandlerListener[] | ||
|
||
constructor(editor: Editor) { | ||
this.editor = editor | ||
this._handlers = [] | ||
} | ||
|
||
register(handler: TextInputHandlerListener) { | ||
assert(`Input Handler is not valid`, this._validateHandler(handler)) | ||
this._handlers.push(handler) | ||
} | ||
|
||
unregister(name: string) { | ||
let handlers = this._handlers | ||
for (let i = 0; i < handlers.length; i++) { | ||
if (handlers[i].name === name) { | ||
handlers.splice(i, 1) | ||
} | ||
} | ||
} | ||
|
||
handle(string: string) { | ||
let { editor } = this | ||
|
||
editor.insertText(string) | ||
|
||
let matchedHandler = this._findHandler() | ||
if (matchedHandler) { | ||
let [handler, matches] = matchedHandler | ||
handler.run(editor, matches) | ||
} | ||
} | ||
|
||
handleNewLine() { | ||
let { editor } = this | ||
|
||
let matchedHandler = this._findHandler(ENTER) | ||
if (matchedHandler) { | ||
let [handler, matches] = matchedHandler | ||
handler.run(editor, matches) | ||
} | ||
} | ||
|
||
_findHandler(string = ''): Maybe<[TextInputHandlerListener, string[]]> { | ||
const { editor } = this | ||
const { range } = editor | ||
const { head } = range | ||
const { section } = head | ||
|
||
let preText = (section! as unknown as Markerable).textUntil(head) + string | ||
|
||
for (let i = 0; i < this._handlers.length; i++) { | ||
let handler = this._handlers[i] | ||
|
||
if (('text' in handler) && endsWith(preText, handler.text)) { | ||
return [handler, [handler.text]] | ||
} else if (('match' in handler) && handler.match.test(preText)) { | ||
return [handler, handler.match.exec(preText)!] | ||
} | ||
} | ||
} | ||
|
||
_validateHandler(handler: TextInputHandlerListener) { | ||
deprecate('Registered input handlers require a "name" property so that they can be unregistered', !!handler.name) | ||
return ( | ||
!!handler.run && // has `run` | ||
(!!(handler as TextHandler).text || !!(handler as MatchHandler).match) && // and `text` or `match` | ||
!(!!(handler as TextHandler).text && !!(handler as MatchHandler).match) | ||
) // not both `text` and `match` | ||
} | ||
|
||
destroy() { | ||
this._handlers = [] | ||
} | ||
} | ||
|
||
interface BaseHandler { | ||
name: string | ||
run: (editor: Editor, matches: string[]) => void | ||
} | ||
|
||
interface TextHandler extends BaseHandler { | ||
text: string | ||
} | ||
|
||
interface MatchHandler extends BaseHandler { | ||
match: RegExp | ||
} | ||
|
||
export type TextInputHandlerListener = TextHandler | MatchHandler | ||
|
||
export default TextInputHandler |
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
Oops, something went wrong.