mirror of https://github.com/vector-im/riot-web
support toggling inline formatting
parent
4876f4c469
commit
26bd694c6a
|
@ -24,7 +24,7 @@ import {setSelection} from '../../../editor/caret';
|
||||||
import {
|
import {
|
||||||
formatRangeAsQuote,
|
formatRangeAsQuote,
|
||||||
formatRangeAsCode,
|
formatRangeAsCode,
|
||||||
formatInline,
|
toggleInlineFormat,
|
||||||
replaceRangeAndMoveCaret,
|
replaceRangeAndMoveCaret,
|
||||||
} from '../../../editor/operations';
|
} from '../../../editor/operations';
|
||||||
import {getCaretOffsetAndText, getRangeForSelection} from '../../../editor/dom';
|
import {getCaretOffsetAndText, getRangeForSelection} from '../../../editor/dom';
|
||||||
|
@ -457,13 +457,13 @@ export default class BasicMessageEditor extends React.Component {
|
||||||
this.historyManager.ensureLastChangesPushed(this.props.model);
|
this.historyManager.ensureLastChangesPushed(this.props.model);
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "bold":
|
case "bold":
|
||||||
formatInline(range, "**");
|
toggleInlineFormat(range, "**");
|
||||||
break;
|
break;
|
||||||
case "italics":
|
case "italics":
|
||||||
formatInline(range, "*");
|
toggleInlineFormat(range, "*");
|
||||||
break;
|
break;
|
||||||
case "strikethrough":
|
case "strikethrough":
|
||||||
formatInline(range, "<del>", "</del>");
|
toggleInlineFormat(range, "<del>", "</del>");
|
||||||
break;
|
break;
|
||||||
case "code":
|
case "code":
|
||||||
formatRangeAsCode(range);
|
formatRangeAsCode(range);
|
||||||
|
|
|
@ -100,10 +100,27 @@ export function formatRangeAsCode(range) {
|
||||||
replaceRangeAndExpandSelection(range, parts);
|
replaceRangeAndExpandSelection(range, parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatInline(range, prefix, suffix = prefix) {
|
export function toggleInlineFormat(range, prefix, suffix = prefix) {
|
||||||
const {model, parts} = range;
|
const {model, parts} = range;
|
||||||
const {partCreator} = model;
|
const {partCreator} = model;
|
||||||
|
|
||||||
|
const isFormatted = parts.length &&
|
||||||
|
parts[0].text.startsWith(prefix) &&
|
||||||
|
parts[parts.length - 1].text.endsWith(suffix);
|
||||||
|
|
||||||
|
if (isFormatted) {
|
||||||
|
// remove prefix and suffix
|
||||||
|
const partWithoutPrefix = parts[0].serialize();
|
||||||
|
partWithoutPrefix.text = partWithoutPrefix.text.substr(prefix.length);
|
||||||
|
parts[0] = partCreator.deserializePart(partWithoutPrefix);
|
||||||
|
|
||||||
|
const partWithoutSuffix = parts[parts.length - 1].serialize();
|
||||||
|
const suffixPartText = partWithoutSuffix.text;
|
||||||
|
partWithoutSuffix.text = suffixPartText.substring(0, suffixPartText.length - suffix.length);
|
||||||
|
parts[parts.length - 1] = partCreator.deserializePart(partWithoutSuffix);
|
||||||
|
} else {
|
||||||
parts.unshift(partCreator.plain(prefix));
|
parts.unshift(partCreator.plain(prefix));
|
||||||
parts.push(partCreator.plain(suffix));
|
parts.push(partCreator.plain(suffix));
|
||||||
|
}
|
||||||
replaceRangeAndExpandSelection(range, parts);
|
replaceRangeAndExpandSelection(range, parts);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue