add support for inline/block code formatting

This commit is contained in:
Bruno Windels 2019-09-04 12:40:21 +02:00
parent b35a3531bb
commit 6e694c113a
2 changed files with 30 additions and 2 deletions

View file

@ -24,6 +24,7 @@ import {setCaretPosition} from '../../../editor/caret';
import {
replaceRangeAndExpandSelection,
formatRangeAsQuote,
formatRangeAsCode,
formatInline,
} from '../../../editor/operations';
import {getCaretOffsetAndText, getRangeForSelection} from '../../../editor/dom';
@ -457,8 +458,12 @@ export default class BasicMessageEditor extends React.Component {
formatRangeAsQuote(range);
}
_formatCodeBlock = () => {
_formatCode = () => {
const range = getRangeForSelection(
this._editorRef,
this.props.model,
document.getSelection());
formatRangeAsCode(range);
}
render() {

View file

@ -67,6 +67,29 @@ export function formatRangeAsQuote(range) {
replaceRangeAndExpandSelection(model, range, parts);
}
export function formatRangeAsCode(range) {
const {model, parts} = range;
const {partCreator} = model;
const needsBlock = parts.some(p => p.type === "newline");
if (needsBlock) {
parts.unshift(partCreator.plain("```"), partCreator.newline());
if (!rangeStartsAtBeginningOfLine(range)) {
parts.unshift(partCreator.newline());
}
parts.push(
partCreator.newline(),
partCreator.plain("```"));
if (rangeEndsAtEndOfLine(range)) {
parts.push(partCreator.newline());
}
replaceRangeAndExpandSelection(model, range, parts);
} else {
parts.unshift(partCreator.plain("`"));
parts.push(partCreator.plain("`"));
replaceRangeAndExpandSelection(model, range, parts);
}
}
export function formatInline(range, prefix, suffix = prefix) {
const {model, parts} = range;
const {partCreator} = model;