-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ui-markdown-editor): inline wysiwyg - #360 #361
Open
d-e-v-esh
wants to merge
3
commits into
accordproject:main
Choose a base branch
from
d-e-v-esh:inline-wysiwyg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
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
161 changes: 137 additions & 24 deletions
161
packages/ui-markdown-editor/src/utilities/matchCases.js
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 |
---|---|---|
@@ -1,35 +1,148 @@ | ||
import { Transforms, Editor } from 'slate'; | ||
import { Transforms, Editor } from "slate"; | ||
import { H1, H2, H3, H4, H5, H6, HR } from "./schema"; | ||
import { insertThematicBreak } from "./toolbarHelpers"; | ||
|
||
export const matchCases = (editor, currentLine) => { | ||
const offsetBeforeSpace = editor.selection.anchor.offset - 2; | ||
const lastChar = currentLine.charAt(offsetBeforeSpace); | ||
const prevTextFromSpace = currentLine.substr(0, offsetBeforeSpace + 1); | ||
|
||
const matchHeadings = (editor, currentLine) => { | ||
const headingMatchCase = currentLine.match(/(^\s*)#{1,6}\s/m); | ||
if(!headingMatchCase) return; | ||
const matchHeadings = () => { | ||
const headingMatchCase = currentLine.match(/(^\s*)#{1,6}\s/m); | ||
if (!headingMatchCase) return; | ||
|
||
const count = (headingMatchCase[0].match(/#/g) || []).length; | ||
if (count === 1) Transforms.setNodes(editor, { type: H1 }); | ||
else if (count === 2) Transforms.setNodes(editor, { type: H2 }); | ||
else if (count === 3) Transforms.setNodes(editor, { type: H3 }); | ||
else if (count === 4) Transforms.setNodes(editor, { type: H4 }); | ||
else if (count === 5) Transforms.setNodes(editor, { type: H5 }); | ||
else if (count === 6) Transforms.setNodes(editor, { type: H6 }); | ||
const count = (headingMatchCase[0].match(/#/g) || []).length; | ||
if (count === 1) Transforms.setNodes(editor, { type: H1 }); | ||
else if (count === 2) Transforms.setNodes(editor, { type: H2 }); | ||
else if (count === 3) Transforms.setNodes(editor, { type: H3 }); | ||
else if (count === 4) Transforms.setNodes(editor, { type: H4 }); | ||
else if (count === 5) Transforms.setNodes(editor, { type: H5 }); | ||
else if (count === 6) Transforms.setNodes(editor, { type: H6 }); | ||
|
||
Editor.deleteBackward(editor, { unit: 'word' }); | ||
return; | ||
} | ||
Editor.deleteBackward(editor, { unit: "word" }); | ||
return; | ||
}; | ||
|
||
const matchPageBreak = (editor, currentLine)=>{ | ||
const pageBreakMatchCase = currentLine.match(/(^\s*)([*-])(?:[\t ]*\2){2,}/m); | ||
if(!pageBreakMatchCase) return | ||
const matchPageBreak = () => { | ||
const pageBreakMatchCase = currentLine.match(/(^\s*)([-])(?:[\t ]*\2){2,}/m); | ||
if (!pageBreakMatchCase) return; | ||
|
||
Editor.deleteBackward(editor, { unit: 'word' }); | ||
insertThematicBreak(editor, HR); | ||
Editor.deleteBackward(editor, { unit: "word" }); | ||
insertThematicBreak(editor, HR); | ||
|
||
return; | ||
} | ||
return; | ||
}; | ||
|
||
matchHeadings(editor, currentLine); | ||
matchPageBreak(editor, currentLine); | ||
} | ||
/** | ||
* | ||
* @param {string} textToInsert The text that we want to format | ||
* @param {Object} textFormats This is the format style of the text (bold, italic, code) we want to apply or remove as the key in an object and a boolean as the value. i.e. `{ bold: true }` | ||
* @param {Integer} markdownCharacters The number of markdown characters that the user has to type to trigger WYSIWYG | ||
*/ | ||
const insertFormattedInlineText = (textToInsert, textFormats, markdownCharacters) => { | ||
const currentRange = { | ||
anchor: editor.selection.anchor, | ||
focus: { | ||
path: editor.selection.focus.path, | ||
offset: | ||
editor.selection.focus.offset - | ||
(textToInsert.length + 1 + markdownCharacters), | ||
}, | ||
}; | ||
|
||
Transforms.insertText(editor, " "); | ||
|
||
Transforms.insertNodes( | ||
editor, | ||
{ | ||
text: textToInsert, | ||
|
||
...textFormats, | ||
}, | ||
{ | ||
at: currentRange, | ||
} | ||
); | ||
}; | ||
|
||
const matchCodeInline = () => { | ||
const codeInlineMatchCase = prevTextFromSpace.match(/\s?(`|``)((?!\1).)+?\1$/m); | ||
if (!lastChar === "`") return; | ||
if (!codeInlineMatchCase) return; | ||
|
||
const codeText = codeInlineMatchCase[0] | ||
.trim() | ||
.replace(new RegExp(codeInlineMatchCase[1], "g"), ""); | ||
|
||
insertFormattedInlineText( | ||
codeText, | ||
{ | ||
code: true, | ||
}, | ||
2 | ||
); | ||
|
||
return; | ||
}; | ||
|
||
const matchBoldAndItalic = () => { | ||
let boldAndItalicMatchCase; | ||
let boldMatchCase; | ||
let italicMatchCase; | ||
|
||
if (lastChar === "*" || lastChar === "_") { | ||
if ( | ||
(boldAndItalicMatchCase = prevTextFromSpace.match( | ||
/\s?(\*\*\*|___)((?!\1).)+?\1$/m | ||
)) | ||
) { | ||
// ***[bold + italic]***, ___[bold + italic]___ | ||
const reg = | ||
boldAndItalicMatchCase[1] === "***" ? /\*\*\*/ : boldAndItalicMatchCase[1]; | ||
const boldAndItalicText = boldAndItalicMatchCase[0] | ||
.trim() | ||
.replace(new RegExp(reg, "g"), ""); | ||
insertFormattedInlineText( | ||
boldAndItalicText, | ||
{ | ||
bold: true, | ||
italic: true, | ||
}, | ||
6 | ||
); | ||
} else if ( | ||
(boldMatchCase = prevTextFromSpace.match(/\s?(\*\*|__)((?!\1).)+?\1$/m)) | ||
) { | ||
// **bold**, __bold__ | ||
const reg = boldMatchCase[1] === "**" ? /\*\*/ : boldMatchCase[1]; | ||
const boldText = boldMatchCase[0].replace(new RegExp(reg, "g"), ""); | ||
insertFormattedInlineText( | ||
boldText, | ||
{ | ||
bold: true, | ||
}, | ||
4 | ||
); | ||
} else if ( | ||
(italicMatchCase = prevTextFromSpace.match(/\s?(\*|_)((?!\1).)+?\1$/m)) | ||
) { | ||
// *italic*, _italic_ | ||
const reg = italicMatchCase[1] === "*" ? /\*/ : italicMatchCase[1]; | ||
const italicText = italicMatchCase[0].replace(new RegExp(reg, "g"), ""); | ||
insertFormattedInlineText( | ||
italicText, | ||
{ | ||
italic: true, | ||
}, | ||
2 | ||
); | ||
} | ||
} | ||
}; | ||
|
||
matchHeadings(); | ||
matchPageBreak(); | ||
matchBoldAndItalic(); | ||
matchCodeInline(); | ||
return; | ||
}; |
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.
Could this be better handled in our
onKeyDown
handler instead of in thewithText
plugin?