|
@@ -265,8 +265,9 @@ ShortcutEventHandler markdownLinkOrImageHandler = (editorState, event) {
|
|
return KeyEventResult.handled;
|
|
return KeyEventResult.handled;
|
|
};
|
|
};
|
|
|
|
|
|
-// convert **abc** to bold abc.
|
|
|
|
-ShortcutEventHandler doubleAsterisksToBold = (editorState, event) {
|
|
|
|
|
|
+ShortcutEventHandler underscoreToItalicHandler = (editorState, event) {
|
|
|
|
+ // Obtain the selection and selected nodes of the current document through the 'selectionService'
|
|
|
|
+ // to determine whether the selection is collapsed and whether the selected node is a text node.
|
|
final selectionService = editorState.service.selectionService;
|
|
final selectionService = editorState.service.selectionService;
|
|
final selection = selectionService.currentSelection.value;
|
|
final selection = selectionService.currentSelection.value;
|
|
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
|
|
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
|
|
@@ -275,53 +276,33 @@ ShortcutEventHandler doubleAsterisksToBold = (editorState, event) {
|
|
}
|
|
}
|
|
|
|
|
|
final textNode = textNodes.first;
|
|
final textNode = textNodes.first;
|
|
- final text = textNode.toPlainText().substring(0, selection.end.offset);
|
|
|
|
-
|
|
|
|
- // make sure the last two characters are **.
|
|
|
|
- if (text.length < 2 || text[selection.end.offset - 1] != '*') {
|
|
|
|
- return KeyEventResult.ignored;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // find all the index of `*`.
|
|
|
|
- final asteriskIndexes = <int>[];
|
|
|
|
- for (var i = 0; i < text.length; i++) {
|
|
|
|
- if (text[i] == '*') {
|
|
|
|
- asteriskIndexes.add(i);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (asteriskIndexes.length < 3) {
|
|
|
|
- return KeyEventResult.ignored;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // make sure the second to last and third to last asterisks are connected.
|
|
|
|
- final thirdToLastAsteriskIndex = asteriskIndexes[asteriskIndexes.length - 3];
|
|
|
|
- final secondToLastAsteriskIndex = asteriskIndexes[asteriskIndexes.length - 2];
|
|
|
|
- final lastAsterisIndex = asteriskIndexes[asteriskIndexes.length - 1];
|
|
|
|
- if (secondToLastAsteriskIndex != thirdToLastAsteriskIndex + 1 ||
|
|
|
|
- lastAsterisIndex == secondToLastAsteriskIndex + 1) {
|
|
|
|
|
|
+ final text = textNode.toPlainText();
|
|
|
|
+ // Determine if an 'underscore' already exists in the text node and only once.
|
|
|
|
+ final firstUnderscore = text.indexOf('_');
|
|
|
|
+ final lastUnderscore = text.lastIndexOf('_');
|
|
|
|
+ if (firstUnderscore == -1 ||
|
|
|
|
+ firstUnderscore != lastUnderscore ||
|
|
|
|
+ firstUnderscore == selection.start.offset - 1) {
|
|
return KeyEventResult.ignored;
|
|
return KeyEventResult.ignored;
|
|
}
|
|
}
|
|
|
|
|
|
- // delete the last three asterisks.
|
|
|
|
- // update the style of the text surround by `** **` to bold.
|
|
|
|
|
|
+ // Delete the previous 'underscore',
|
|
|
|
+ // update the style of the text surrounded by the two underscores to 'italic',
|
|
// and update the cursor position.
|
|
// and update the cursor position.
|
|
final transaction = editorState.transaction
|
|
final transaction = editorState.transaction
|
|
- ..deleteText(textNode, lastAsterisIndex, 1)
|
|
|
|
- ..deleteText(textNode, thirdToLastAsteriskIndex, 2)
|
|
|
|
|
|
+ ..deleteText(textNode, firstUnderscore, 1)
|
|
..formatText(
|
|
..formatText(
|
|
textNode,
|
|
textNode,
|
|
- thirdToLastAsteriskIndex,
|
|
|
|
- selection.end.offset - thirdToLastAsteriskIndex - 3,
|
|
|
|
|
|
+ firstUnderscore,
|
|
|
|
+ selection.end.offset - firstUnderscore - 1,
|
|
{
|
|
{
|
|
- BuiltInAttributeKey.bold: true,
|
|
|
|
- BuiltInAttributeKey.defaultFormating: true,
|
|
|
|
|
|
+ BuiltInAttributeKey.italic: true,
|
|
},
|
|
},
|
|
)
|
|
)
|
|
..afterSelection = Selection.collapsed(
|
|
..afterSelection = Selection.collapsed(
|
|
Position(
|
|
Position(
|
|
path: textNode.path,
|
|
path: textNode.path,
|
|
- offset: selection.end.offset - 3,
|
|
|
|
|
|
+ offset: selection.end.offset - 1,
|
|
),
|
|
),
|
|
);
|
|
);
|
|
editorState.apply(transaction);
|
|
editorState.apply(transaction);
|
|
@@ -329,111 +310,115 @@ ShortcutEventHandler doubleAsterisksToBold = (editorState, event) {
|
|
return KeyEventResult.handled;
|
|
return KeyEventResult.handled;
|
|
};
|
|
};
|
|
|
|
|
|
-// convert __abc__ to bold abc.
|
|
|
|
-ShortcutEventHandler doubleUnderscoresToBold = (editorState, event) {
|
|
|
|
|
|
+ShortcutEventHandler doubleAsteriskToBoldHanlder = (editorState, event) {
|
|
final selectionService = editorState.service.selectionService;
|
|
final selectionService = editorState.service.selectionService;
|
|
final selection = selectionService.currentSelection.value;
|
|
final selection = selectionService.currentSelection.value;
|
|
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
|
|
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
|
|
|
|
+
|
|
if (selection == null || !selection.isSingle || textNodes.length != 1) {
|
|
if (selection == null || !selection.isSingle || textNodes.length != 1) {
|
|
return KeyEventResult.ignored;
|
|
return KeyEventResult.ignored;
|
|
}
|
|
}
|
|
|
|
|
|
final textNode = textNodes.first;
|
|
final textNode = textNodes.first;
|
|
- final text = textNode.toPlainText().substring(0, selection.end.offset);
|
|
|
|
|
|
+ final text = textNode.toPlainText();
|
|
|
|
|
|
- // make sure the last two characters are __.
|
|
|
|
- if (text.length < 2 || text[selection.end.offset - 1] != '_') {
|
|
|
|
|
|
+// make sure the last two characters are '**'
|
|
|
|
+ if (text.length < 2 || text[selection.end.offset - 1] != '*') {
|
|
return KeyEventResult.ignored;
|
|
return KeyEventResult.ignored;
|
|
}
|
|
}
|
|
|
|
|
|
- // find all the index of `_`.
|
|
|
|
- final underscoreIndexes = <int>[];
|
|
|
|
|
|
+// find all the index of '*'
|
|
|
|
+ final asteriskIndexList = <int>[];
|
|
for (var i = 0; i < text.length; i++) {
|
|
for (var i = 0; i < text.length; i++) {
|
|
- if (text[i] == '_') {
|
|
|
|
- underscoreIndexes.add(i);
|
|
|
|
|
|
+ if (text[i] == '*') {
|
|
|
|
+ asteriskIndexList.add(i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if (underscoreIndexes.length < 3) {
|
|
|
|
- return KeyEventResult.ignored;
|
|
|
|
- }
|
|
|
|
|
|
+ if (asteriskIndexList.length < 3) return KeyEventResult.ignored;
|
|
|
|
|
|
- // make sure the second to last and third to last underscores are connected.
|
|
|
|
- final thirdToLastUnderscoreIndex =
|
|
|
|
- underscoreIndexes[underscoreIndexes.length - 3];
|
|
|
|
- final secondToLastUnderscoreIndex =
|
|
|
|
- underscoreIndexes[underscoreIndexes.length - 2];
|
|
|
|
- final lastAsterisIndex = underscoreIndexes[underscoreIndexes.length - 1];
|
|
|
|
- if (secondToLastUnderscoreIndex != thirdToLastUnderscoreIndex + 1 ||
|
|
|
|
- lastAsterisIndex == secondToLastUnderscoreIndex + 1) {
|
|
|
|
|
|
+// make sure the second to last and third to last asterisk are connected
|
|
|
|
+ final thirdToLastAsteriskIndex =
|
|
|
|
+ asteriskIndexList[asteriskIndexList.length - 3];
|
|
|
|
+ final secondToLastAsteriskIndex =
|
|
|
|
+ asteriskIndexList[asteriskIndexList.length - 2];
|
|
|
|
+ final lastAsteriskIndex = asteriskIndexList[asteriskIndexList.length - 1];
|
|
|
|
+ if (secondToLastAsteriskIndex != thirdToLastAsteriskIndex + 1 ||
|
|
|
|
+ lastAsteriskIndex == secondToLastAsteriskIndex + 1) {
|
|
return KeyEventResult.ignored;
|
|
return KeyEventResult.ignored;
|
|
}
|
|
}
|
|
|
|
|
|
- // delete the last three underscores.
|
|
|
|
- // update the style of the text surround by `__ __` to bold.
|
|
|
|
- // and update the cursor position.
|
|
|
|
|
|
+//delete the last three asterisks
|
|
|
|
+//update the style of the text surround by '** **' to bold
|
|
|
|
+//update the cursor position
|
|
final transaction = editorState.transaction
|
|
final transaction = editorState.transaction
|
|
- ..deleteText(textNode, lastAsterisIndex, 1)
|
|
|
|
- ..deleteText(textNode, thirdToLastUnderscoreIndex, 2)
|
|
|
|
- ..formatText(
|
|
|
|
- textNode,
|
|
|
|
- thirdToLastUnderscoreIndex,
|
|
|
|
- selection.end.offset - thirdToLastUnderscoreIndex - 3,
|
|
|
|
- {
|
|
|
|
- BuiltInAttributeKey.bold: true,
|
|
|
|
- BuiltInAttributeKey.defaultFormating: true,
|
|
|
|
- },
|
|
|
|
- )
|
|
|
|
|
|
+ ..deleteText(textNode, lastAsteriskIndex, 1)
|
|
|
|
+ ..deleteText(textNode, thirdToLastAsteriskIndex, 2)
|
|
|
|
+ ..formatText(textNode, thirdToLastAsteriskIndex,
|
|
|
|
+ selection.end.offset - thirdToLastAsteriskIndex - 2, {
|
|
|
|
+ BuiltInAttributeKey.bold: true,
|
|
|
|
+ })
|
|
..afterSelection = Selection.collapsed(
|
|
..afterSelection = Selection.collapsed(
|
|
- Position(
|
|
|
|
- path: textNode.path,
|
|
|
|
- offset: selection.end.offset - 3,
|
|
|
|
- ),
|
|
|
|
- );
|
|
|
|
|
|
+ Position(path: textNode.path, offset: selection.end.offset - 3));
|
|
|
|
+
|
|
editorState.apply(transaction);
|
|
editorState.apply(transaction);
|
|
|
|
+
|
|
return KeyEventResult.handled;
|
|
return KeyEventResult.handled;
|
|
};
|
|
};
|
|
|
|
|
|
-ShortcutEventHandler underscoreToItalicHandler = (editorState, event) {
|
|
|
|
- // Obtain the selection and selected nodes of the current document through the 'selectionService'
|
|
|
|
- // to determine whether the selection is collapsed and whether the selected node is a text node.
|
|
|
|
|
|
+//Implement in the same way as doubleAsteriskToBoldHanlder
|
|
|
|
+ShortcutEventHandler doubleUnderscoreToBoldHanlder = (editorState, event) {
|
|
final selectionService = editorState.service.selectionService;
|
|
final selectionService = editorState.service.selectionService;
|
|
final selection = selectionService.currentSelection.value;
|
|
final selection = selectionService.currentSelection.value;
|
|
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
|
|
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
|
|
|
|
+
|
|
if (selection == null || !selection.isSingle || textNodes.length != 1) {
|
|
if (selection == null || !selection.isSingle || textNodes.length != 1) {
|
|
return KeyEventResult.ignored;
|
|
return KeyEventResult.ignored;
|
|
}
|
|
}
|
|
|
|
|
|
final textNode = textNodes.first;
|
|
final textNode = textNodes.first;
|
|
final text = textNode.toPlainText();
|
|
final text = textNode.toPlainText();
|
|
- // Determine if an 'underscore' already exists in the text node and only once.
|
|
|
|
- final firstUnderscore = text.indexOf('_');
|
|
|
|
- final lastUnderscore = text.lastIndexOf('_');
|
|
|
|
- if (firstUnderscore == -1 ||
|
|
|
|
- firstUnderscore != lastUnderscore ||
|
|
|
|
- firstUnderscore == selection.start.offset - 1) {
|
|
|
|
|
|
+
|
|
|
|
+// make sure the last two characters are '__'
|
|
|
|
+ if (text.length < 2 || text[selection.end.offset - 1] != '_') {
|
|
return KeyEventResult.ignored;
|
|
return KeyEventResult.ignored;
|
|
}
|
|
}
|
|
|
|
|
|
- // Delete the previous 'underscore',
|
|
|
|
- // update the style of the text surrounded by the two underscores to 'italic',
|
|
|
|
- // and update the cursor position.
|
|
|
|
|
|
+// find all the index of '_'
|
|
|
|
+ final underscoreIndexList = <int>[];
|
|
|
|
+ for (var i = 0; i < text.length; i++) {
|
|
|
|
+ if (text[i] == '_') {
|
|
|
|
+ underscoreIndexList.add(i);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (underscoreIndexList.length < 3) return KeyEventResult.ignored;
|
|
|
|
+
|
|
|
|
+// make sure the second to last and third to last underscore are connected
|
|
|
|
+ final thirdToLastUnderscoreIndex =
|
|
|
|
+ underscoreIndexList[underscoreIndexList.length - 3];
|
|
|
|
+ final secondToLastUnderscoreIndex =
|
|
|
|
+ underscoreIndexList[underscoreIndexList.length - 2];
|
|
|
|
+ final lastUnderscoreIndex =
|
|
|
|
+ underscoreIndexList[underscoreIndexList.length - 1];
|
|
|
|
+ if (secondToLastUnderscoreIndex != thirdToLastUnderscoreIndex + 1 ||
|
|
|
|
+ lastUnderscoreIndex == secondToLastUnderscoreIndex + 1) {
|
|
|
|
+ return KeyEventResult.ignored;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+//delete the last three underscores
|
|
|
|
+//update the style of the text surround by '__ __' to bold
|
|
|
|
+//update the cursor position
|
|
final transaction = editorState.transaction
|
|
final transaction = editorState.transaction
|
|
- ..deleteText(textNode, firstUnderscore, 1)
|
|
|
|
- ..formatText(
|
|
|
|
- textNode,
|
|
|
|
- firstUnderscore,
|
|
|
|
- selection.end.offset - firstUnderscore - 1,
|
|
|
|
- {
|
|
|
|
- BuiltInAttributeKey.italic: true,
|
|
|
|
- },
|
|
|
|
- )
|
|
|
|
|
|
+ ..deleteText(textNode, lastUnderscoreIndex, 1)
|
|
|
|
+ ..deleteText(textNode, thirdToLastUnderscoreIndex, 2)
|
|
|
|
+ ..formatText(textNode, thirdToLastUnderscoreIndex,
|
|
|
|
+ selection.end.offset - thirdToLastUnderscoreIndex - 2, {
|
|
|
|
+ BuiltInAttributeKey.bold: true,
|
|
|
|
+ })
|
|
..afterSelection = Selection.collapsed(
|
|
..afterSelection = Selection.collapsed(
|
|
- Position(
|
|
|
|
- path: textNode.path,
|
|
|
|
- offset: selection.end.offset - 1,
|
|
|
|
- ),
|
|
|
|
- );
|
|
|
|
|
|
+ Position(path: textNode.path, offset: selection.end.offset - 3));
|
|
|
|
+
|
|
editorState.apply(transaction);
|
|
editorState.apply(transaction);
|
|
|
|
|
|
return KeyEventResult.handled;
|
|
return KeyEventResult.handled;
|