You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-11-08 21:42:24 +03:00
add support for inline/block code formatting
This commit is contained in:
@@ -24,6 +24,7 @@ import {setCaretPosition} from '../../../editor/caret';
|
|||||||
import {
|
import {
|
||||||
replaceRangeAndExpandSelection,
|
replaceRangeAndExpandSelection,
|
||||||
formatRangeAsQuote,
|
formatRangeAsQuote,
|
||||||
|
formatRangeAsCode,
|
||||||
formatInline,
|
formatInline,
|
||||||
} from '../../../editor/operations';
|
} from '../../../editor/operations';
|
||||||
import {getCaretOffsetAndText, getRangeForSelection} from '../../../editor/dom';
|
import {getCaretOffsetAndText, getRangeForSelection} from '../../../editor/dom';
|
||||||
@@ -457,8 +458,12 @@ export default class BasicMessageEditor extends React.Component {
|
|||||||
formatRangeAsQuote(range);
|
formatRangeAsQuote(range);
|
||||||
}
|
}
|
||||||
|
|
||||||
_formatCodeBlock = () => {
|
_formatCode = () => {
|
||||||
|
const range = getRangeForSelection(
|
||||||
|
this._editorRef,
|
||||||
|
this.props.model,
|
||||||
|
document.getSelection());
|
||||||
|
formatRangeAsCode(range);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@@ -67,6 +67,29 @@ export function formatRangeAsQuote(range) {
|
|||||||
replaceRangeAndExpandSelection(model, range, parts);
|
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) {
|
export function formatInline(range, prefix, suffix = prefix) {
|
||||||
const {model, parts} = range;
|
const {model, parts} = range;
|
||||||
const {partCreator} = model;
|
const {partCreator} = model;
|
||||||
|
|||||||
Reference in New Issue
Block a user