-
Notifications
You must be signed in to change notification settings - Fork 167
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
1 parent
b1848b7
commit 9a7e983
Showing
7 changed files
with
110 additions
and
15 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
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
57 changes: 57 additions & 0 deletions
57
...oosterjs-editor-plugins/lib/ContentEdit/features/insertLineBeforeStructuredNodeFeature.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,57 @@ | ||
import { cacheGetEventData, ContentEditFeature, Editor, Keys } from 'roosterjs-editor-core'; | ||
import { PluginKeyboardEvent, PositionType } from 'roosterjs-editor-types'; | ||
import { | ||
Browser, | ||
fromHtml, | ||
isPositionAtBeginningOf, | ||
Position, | ||
getTagOfNode, | ||
} from 'roosterjs-editor-dom'; | ||
|
||
// Edge can sometimes lose current format when Enter to new line. | ||
// So here we add an extra SPAN for Edge to workaround this bug | ||
const NEWLINE_HTML = Browser.isEdge ? '<div><span><br></span></div>' : '<div><br></div>'; | ||
const CHILD_PARENT_TAG_MAP: { [childTag: string]: string } = { | ||
TD: 'TABLE', | ||
TH: 'TABLE', | ||
LI: 'OL,UL', | ||
}; | ||
const CHILD_SELECTOR = Object.keys(CHILD_PARENT_TAG_MAP).join(','); | ||
|
||
export const InsertLineBeforeStructuredNodeFeature: ContentEditFeature = { | ||
keys: [Keys.ENTER], | ||
shouldHandleEvent: cacheGetStructuredElement, | ||
handleEvent: (event, editor) => { | ||
let element = cacheGetStructuredElement(event, editor); | ||
let div = fromHtml(NEWLINE_HTML, editor.getDocument())[0] as HTMLElement; | ||
editor.addUndoSnapshot(() => { | ||
element.parentNode.insertBefore(div, element); | ||
// Select the new line when we are in table. This is the same behavior with Word | ||
if (getTagOfNode(element) == 'TABLE') { | ||
editor.select(new Position(div, PositionType.Begin).normalize()); | ||
} | ||
}); | ||
event.rawEvent.preventDefault(); | ||
}, | ||
}; | ||
|
||
function cacheGetStructuredElement(event: PluginKeyboardEvent, editor: Editor) { | ||
return cacheGetEventData(event, 'FIRST_STRUCTURE', () => { | ||
// Provide a chance to keep browser default behavior by pressing SHIFT | ||
let element = event.rawEvent.shiftKey ? null : editor.getElementAtCursor(CHILD_SELECTOR); | ||
|
||
if (element) { | ||
let range = editor.getSelectionRange(); | ||
if ( | ||
range && | ||
range.collapsed && | ||
isPositionAtBeginningOf(Position.getStart(range), element) && | ||
!editor.getBodyTraverser(element).getPreviousBlockElement() | ||
) { | ||
return editor.getElementAtCursor(CHILD_PARENT_TAG_MAP[getTagOfNode(element)]); | ||
} | ||
} | ||
|
||
return null; | ||
}); | ||
} |
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