-
Notifications
You must be signed in to change notification settings - Fork 178
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
9397a2e
commit eb3a6f1
Showing
2 changed files
with
24 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import * as React from "react"; | ||
import {Command} from "../types"; | ||
import {TextApi, TextState} from "../types/CommandOptions"; | ||
import {selectWord} from "../util/MarkdownUtil"; | ||
import { Command } from "../types"; | ||
import { TextApi, TextState } from "../types/CommandOptions"; | ||
import { selectWord } from "../util/MarkdownUtil"; | ||
|
||
export const imageCommand: Command = { | ||
name: "image", | ||
buttonProps: {"aria-label": "Add image"}, | ||
execute: (state0: TextState, api: TextApi) => { | ||
// Adjust the selection to encompass the whole word if the caret is inside one | ||
const newSelectionRange = selectWord({text: state0.text, selection: state0.selection}); | ||
const state1 = api.setSelectionRange(newSelectionRange); | ||
// Replaces the current selection with the bold mark up | ||
const state2 = api.replaceSelection(`![${state1.selectedText}](image-url)`); | ||
// Adjust the selection to not contain the ** | ||
api.setSelectionRange({ | ||
start: state2.selection.end - 12 - state1.selectedText.length, | ||
end: state2.selection.end - 12 | ||
}); | ||
}, | ||
keyCommand: "image", | ||
name: "image", | ||
buttonProps: { "aria-label": "Add image" }, | ||
execute: (state0: TextState, api: TextApi) => { | ||
// Select everything | ||
const newSelectionRange = selectWord({ text: state0.text, selection: state0.selection }); | ||
const state1 = api.setSelectionRange(newSelectionRange); | ||
// Replaces the current selection with the image | ||
const imageTemplate = state1.selectedText || "https://example.com/your-image.png"; | ||
api.replaceSelection(`![](${imageTemplate})`); | ||
// Adjust the selection to not contain the ** | ||
api.setSelectionRange({ | ||
start: 4 + state1.selection.start, | ||
end: 4 + state1.selection.start + imageTemplate.length | ||
}); | ||
}, | ||
keyCommand: "image" | ||
}; |
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