Merge pull request #5263 from matrix-org/t3chguy/fix/15310
Trim range when formatting so that it excludes leading/trailing spacespull/21833/head
commit
e51136f902
|
@ -619,13 +619,14 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
|
||||||
}
|
}
|
||||||
|
|
||||||
private onFormatAction = (action: Formatting) => {
|
private onFormatAction = (action: Formatting) => {
|
||||||
const range = getRangeForSelection(
|
const range = getRangeForSelection(this.editorRef.current, this.props.model, document.getSelection());
|
||||||
this.editorRef.current,
|
// trim the range as we want it to exclude leading/trailing spaces
|
||||||
this.props.model,
|
range.trim();
|
||||||
document.getSelection());
|
|
||||||
if (range.length === 0) {
|
if (range.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.historyManager.ensureLastChangesPushed(this.props.model);
|
this.historyManager.ensureLastChangesPushed(this.props.model);
|
||||||
this.modifiedFlag = true;
|
this.modifiedFlag = true;
|
||||||
switch (action) {
|
switch (action) {
|
||||||
|
|
|
@ -18,6 +18,10 @@ import EditorModel from "./model";
|
||||||
import DocumentPosition, {Predicate} from "./position";
|
import DocumentPosition, {Predicate} from "./position";
|
||||||
import {Part} from "./parts";
|
import {Part} from "./parts";
|
||||||
|
|
||||||
|
const whitespacePredicate: Predicate = (index, offset, part) => {
|
||||||
|
return part.text[offset].trim() === "";
|
||||||
|
};
|
||||||
|
|
||||||
export default class Range {
|
export default class Range {
|
||||||
private _start: DocumentPosition;
|
private _start: DocumentPosition;
|
||||||
private _end: DocumentPosition;
|
private _end: DocumentPosition;
|
||||||
|
@ -35,6 +39,11 @@ export default class Range {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trim() {
|
||||||
|
this._start = this._start.forwardsWhile(this.model, whitespacePredicate);
|
||||||
|
this._end = this._end.backwardsWhile(this.model, whitespacePredicate);
|
||||||
|
}
|
||||||
|
|
||||||
expandBackwardsWhile(predicate: Predicate) {
|
expandBackwardsWhile(predicate: Predicate) {
|
||||||
this._start = this._start.backwardsWhile(this.model, predicate);
|
this._start = this._start.backwardsWhile(this.model, predicate);
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,4 +88,19 @@ describe('editor/range', function() {
|
||||||
expect(model.parts[1].text).toBe("man");
|
expect(model.parts[1].text).toBe("man");
|
||||||
expect(model.parts.length).toBe(2);
|
expect(model.parts.length).toBe(2);
|
||||||
});
|
});
|
||||||
|
it('range trim spaces off both ends', () => {
|
||||||
|
const renderer = createRenderer();
|
||||||
|
const pc = createPartCreator();
|
||||||
|
const model = new EditorModel([
|
||||||
|
pc.plain("abc abc abc"),
|
||||||
|
], pc, renderer);
|
||||||
|
const range = model.startRange(
|
||||||
|
model.positionForOffset(3, false), // at end of first `abc`
|
||||||
|
model.positionForOffset(8, false), // at start of last `abc`
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(range.parts[0].text).toBe(" abc ");
|
||||||
|
range.trim();
|
||||||
|
expect(range.parts[0].text).toBe("abc");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue