Skip to content

Commit

Permalink
Add codeblock
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerpena committed May 2, 2022
1 parent 9fbceff commit 0c4ebfe
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/commands/markdown-commands/codeBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as React from "react";
import { Command } from "../command";
import { textHelpers } from "../../helpers/textHelpers";

export const codeBlock: Command = {
execute: async ({ textApi, initialState }) => {
// Adjust the selection to encompass the whole word if the caret is inside one
const newSelectionRange = textHelpers.selectWord({
text: initialState.text,
selection: initialState.selection
});
const state1 = textApi.setSelectionRange(newSelectionRange);

// when there's no breaking line
if (textHelpers.getSelectedText(state1).indexOf("\n") === -1) {
textApi.replaceSelection(`\`${textHelpers.getSelectedText(state1)}\``);
// Adjust the selection to not contain the **

const selectionStart = state1.selection.start + 1;
const selectionEnd =
selectionStart + textHelpers.getSelectedText(state1).length;

textApi.setSelectionRange({
start: selectionStart,
end: selectionEnd
});
return;
}

const breaksBeforeCount = textHelpers.getBreaksNeededForEmptyLineBefore(
state1.text,
state1.selection.start
);
const breaksBefore = Array(breaksBeforeCount + 1).join("\n");

const breaksAfterCount = textHelpers.getBreaksNeededForEmptyLineAfter(
state1.text,
state1.selection.end
);
const breaksAfter = Array(breaksAfterCount + 1).join("\n");

textApi.replaceSelection(
`${breaksBefore}\`\`\`\n${textHelpers.getSelectedText(
state1
)}\n\`\`\`${breaksAfter}`
);

const selectionStart = state1.selection.start + breaksBeforeCount + 4;
const selectionEnd =
selectionStart + textHelpers.getSelectedText(state1).length;

textApi.setSelectionRange({
start: selectionStart,
end: selectionEnd
});
}
};

0 comments on commit 0c4ebfe

Please sign in to comment.