Skip to content

Commit 6e557bc

Browse files
committed
Fix the bold command
1 parent 58d5734 commit 6e557bc

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

src/ReactMde.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { extractCommandMap } from "./util/CommandUtils";
1111
import { Tab } from "./types/Tab";
1212
import { L18n } from "./types/L18n";
1313
import { enL18n } from "./l18n/react-mde.en";
14-
import { CommandOrchestrator } from "./commandOrchestrator";
14+
import { CommandOrchestrator, TextAreaCommandOrchestrator } from "./commandOrchestrator";
1515

1616
export interface ReactMdeProps {
1717
value: string;
@@ -140,6 +140,11 @@ export class ReactMde extends React.Component<ReactMdeProps, ReactMdeState> {
140140
document.addEventListener<"mouseup">("mouseup", this.handleGripMouseUp);
141141
}
142142

143+
setTextAreaRef = (element: HTMLTextAreaElement) => {
144+
this.textAreaRef = element;
145+
this.commandOrchestrator = new TextAreaCommandOrchestrator(this.textAreaRef);
146+
};
147+
143148
handleCommand = (command: Command) => {
144149
this.commandOrchestrator.executeCommand(command);
145150
};
@@ -173,7 +178,7 @@ export class ReactMde extends React.Component<ReactMdeProps, ReactMdeState> {
173178
this.state.currentTab === "write" ?
174179
<>
175180
<TextArea
176-
editorRef={(c: HTMLTextAreaElement) => this.textAreaRef = c}
181+
editorRef={this.setTextAreaRef}
177182
onChange={this.handleTextChange}
178183
readOnly={readOnly}
179184
textAreaProps={textAreaProps}

src/commandOrchestrator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function getStateFromTextArea (textArea: HTMLTextAreaElement): TextState
3333
end: textArea.selectionEnd
3434
},
3535
text: textArea.value,
36-
selectedText: textArea.value.slice(textArea.selectionStart, textArea.selectionEnd - 1)
36+
selectedText: textArea.value.slice(textArea.selectionStart, textArea.selectionEnd)
3737
};
3838
}
3939

src/commands/boldCommand.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import { selectWord } from "../util/MarkdownUtil";
66
export const boldCommand: Command = {
77
buttonContentBuilder: ({ iconProvider }) => iconProvider("bold"),
88
buttonProps: { "aria-label": "Add bold text" },
9-
execute: (initialState: TextState, api: TextApi) => {
9+
execute: (state0: TextState, api: TextApi) => {
1010
// Adjust the selection to encompass the whole word if the caret is inside one
11-
let state = api.setSelectionRange(selectWord({ text: initialState.text, selection: initialState.selection }));
11+
const newSelectionRange = selectWord({ text: state0.text, selection: state0.selection });
12+
const state1 = api.setSelectionRange(newSelectionRange);
1213
// Replaces the current selection with the bold mark up
13-
state = api.replaceSelection(`**${state.selectedText}**`);
14+
const state2 = api.replaceSelection(`**${state1.selectedText}**`);
1415
// Adjust the selection to not contain the **
15-
api.setSelectionRange({ start: state.selection.start + 2, end: state.selection.end + 2 });
16+
api.setSelectionRange({
17+
start: state2.selection.end - 2 - state1.selectedText.length,
18+
end: state2.selection.end - 2
19+
});
1620
},
1721
keyCommand: "bold"
1822
};

src/types/CommandOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export interface TextState {
77
}
88

99
export interface TextApi {
10+
/**
11+
* Replaces the current selection with the new text. This will make the new selectedText to be empty, the
12+
* selection start and selection end will be the same and will both point to the end
13+
* @param text
14+
*/
1015
replaceSelection (text: string): TextState;
1116

1217
setSelectionRange (selection: TextRange): TextState;

0 commit comments

Comments
 (0)