-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standalone Editor: Support keyboard input (init step) (#2221)
- Loading branch information
1 parent
3f7cba1
commit 1e5c3bd
Showing
6 changed files
with
414 additions
and
45 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
48 changes: 48 additions & 0 deletions
48
packages-content-model/roosterjs-content-model-plugins/lib/edit/keyboardInput.ts
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,48 @@ | ||
import { deleteSelection, isModifierKey } from 'roosterjs-content-model-core'; | ||
import type { IContentModelEditor } from 'roosterjs-content-model-editor'; | ||
import type { DOMSelection } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function keyboardInput(editor: IContentModelEditor, rawEvent: KeyboardEvent) { | ||
const selection = editor.getDOMSelection(); | ||
|
||
if (shouldInputWithContentModel(selection, rawEvent)) { | ||
editor.addUndoSnapshot(); | ||
|
||
editor.formatContentModel( | ||
(model, context) => { | ||
const result = deleteSelection(model, [], context).deleteResult; | ||
|
||
// We have deleted selection then we will let browser to handle the input. | ||
// With this combined operation, we don't wan to mass up the cached model so clear it | ||
context.clearModelCache = true; | ||
|
||
// Skip undo snapshot here and add undo snapshot before the operation so that we don't add another undo snapshot in middle of this replace operation | ||
context.skipUndoSnapshot = true; | ||
|
||
// Do not preventDefault since we still want browser to handle the final input for now | ||
return result == 'range'; | ||
}, | ||
{ | ||
rawEvent, | ||
} | ||
); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
function shouldInputWithContentModel(selection: DOMSelection | null, rawEvent: KeyboardEvent) { | ||
if (!selection) { | ||
return false; // Nothing to delete | ||
} else if ( | ||
!isModifierKey(rawEvent) && | ||
(rawEvent.key == 'Enter' || rawEvent.key == 'Space' || rawEvent.key.length == 1) | ||
) { | ||
return selection.type != 'range' || !selection.range.collapsed; // TODO: Also handle Enter key even selection is collapsed | ||
} else { | ||
return false; | ||
} | ||
} |
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
Oops, something went wrong.