This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
forked from ngs/draft-js-markdown-shortcuts-plugin
-
Notifications
You must be signed in to change notification settings - Fork 42
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,6 +202,27 @@ function checkReturnForState(config, editorState, ev) { | |
return newEditorState; | ||
} | ||
|
||
const unstickyInlineStyles = (character, editorState) => { | ||
const selection = editorState.getSelection(); | ||
if (!selection.isCollapsed()) return editorState; | ||
|
||
const startOffset = selection.getStartOffset(); | ||
const content = editorState.getCurrentContent(); | ||
const block = content.getBlockForKey(selection.getStartKey()); | ||
const entity = block.getEntityAt(startOffset - 1); | ||
if (entity !== null) return editorState; | ||
|
||
// If we're currently in a style, but the next character doesn't have a style (or doesn't exist) | ||
// we insert the characters manually without the inline style to "unsticky" them | ||
const style = block.getInlineStyleAt(startOffset - 1); | ||
if (style.size === 0) return editorState; | ||
const nextStyle = block.getInlineStyleAt(startOffset); | ||
if (nextStyle.size !== 0) return editorState; | ||
|
||
const newContent = Modifier.insertText(content, selection, character); | ||
return EditorState.push(editorState, newContent, "insert-characters"); | ||
}; | ||
|
||
const defaultConfig = { | ||
renderLanguageSelect: defaultRenderSelect, | ||
languages: defaultLanguages, | ||
|
@@ -347,6 +368,12 @@ const createMarkdownPlugin = (_config = {}) => { | |
// If we're in a link - don't transform markdown | ||
if (inLink(editorState)) return "not-handled"; | ||
|
||
const unsticky = unstickyInlineStyles(character, editorState); | ||
if (editorState !== unsticky) { | ||
setEditorState(unsticky); | ||
return "handled"; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be nice to add a bunch of unit tests for this 👍 |
||
|
||
const newEditorState = checkCharacterForState( | ||
config, | ||
editorState, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also add some unit tests for when stickiness doesn't apply: like when you're in an entity for example 👍 just to make sure it doesn't break in future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried for like an hour to write another test, but it always has the wrong content state (
text: altered!!
) and I don't know where it's coming from or how to fix it 😢 Any clues?