PR feedback, cleanup

This commit is contained in:
Bruno Windels 2019-05-15 09:46:08 +01:00
parent dc21faa240
commit d83e278f6b
4 changed files with 8 additions and 57 deletions

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2019 Vector Creations Ltd Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -19,7 +19,7 @@ limitations under the License.
background-color: #f3f8fd; background-color: #f3f8fd;
padding: 11px 13px 7px 56px; padding: 11px 13px 7px 56px;
.editor { .mx_MessageEditor_editor {
border-radius: 4px; border-radius: 4px;
border: solid 1px #e9edf1; border: solid 1px #e9edf1;
background-color: #ffffff; background-color: #ffffff;
@ -45,7 +45,7 @@ limitations under the License.
} }
} }
.buttons { .mx_MessageEditor_buttons {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: end; justify-content: end;
@ -57,14 +57,6 @@ limitations under the License.
} }
} }
.model {
background: lightgrey;
padding: 5px;
display: none;
white-space: pre;
font-size: 12px;
}
.mx_MessageEditor_AutoCompleteWrapper { .mx_MessageEditor_AutoCompleteWrapper {
position: relative; position: relative;
height: 0; height: 0;

View file

@ -24,16 +24,14 @@ import {getCaretOffsetAndText} from '../../../editor/dom';
import {htmlSerialize, textSerialize, requiresHtml} from '../../../editor/serialize'; import {htmlSerialize, textSerialize, requiresHtml} from '../../../editor/serialize';
import {parseEvent} from '../../../editor/deserialize'; import {parseEvent} from '../../../editor/deserialize';
import Autocomplete from '../rooms/Autocomplete'; import Autocomplete from '../rooms/Autocomplete';
// import AutocompleteModel from '../../../editor/autocomplete';
import {PartCreator} from '../../../editor/parts'; import {PartCreator} from '../../../editor/parts';
import {renderModel} from '../../../editor/render'; import {renderModel} from '../../../editor/render';
import {MatrixEvent, MatrixClient} from 'matrix-js-sdk'; import {MatrixEvent, MatrixClient} from 'matrix-js-sdk';
export default class MessageEditor extends React.Component { export default class MessageEditor extends React.Component {
static propTypes = { static propTypes = {
// the latest event in this chain of replies // the message event being edited
event: PropTypes.instanceOf(MatrixEvent).isRequired, event: PropTypes.instanceOf(MatrixEvent).isRequired,
// onHeightChanged: PropTypes.func.isRequired,
}; };
static contextTypes = { static contextTypes = {
@ -70,13 +68,10 @@ export default class MessageEditor extends React.Component {
} }
} }
this.setState({autoComplete: this.model.autoComplete}); this.setState({autoComplete: this.model.autoComplete});
// const modelOutput = this._editorRef.parentElement.querySelector(".model");
// modelOutput.textContent = JSON.stringify(this.model.serializeParts(), undefined, 2);
} }
_onInput = (event) => { _onInput = (event) => {
const sel = document.getSelection(); const sel = document.getSelection();
// console.log("finding newValue", this._editorRef.innerHTML, sel);
const {caret, text} = getCaretOffsetAndText(this._editorRef, sel); const {caret, text} = getCaretOffsetAndText(this._editorRef, sel);
this.model.update(text, event.inputType, caret); this.model.update(text, event.inputType, caret);
} }
@ -133,14 +128,6 @@ export default class MessageEditor extends React.Component {
dis.dispatch({action: "edit_event", event: null}); dis.dispatch({action: "edit_event", event: null});
} }
_collectEditorRef = (ref) => {
this._editorRef = ref;
}
_collectAutocompleteRef = (ref) => {
this._autocompleteRef = ref;
}
_onAutoCompleteConfirm = (completion) => { _onAutoCompleteConfirm = (completion) => {
this.model.autoComplete.onComponentConfirm(completion); this.model.autoComplete.onComponentConfirm(completion);
} }
@ -160,7 +147,7 @@ export default class MessageEditor extends React.Component {
const queryLen = query.length; const queryLen = query.length;
autoComplete = <div className="mx_MessageEditor_AutoCompleteWrapper"> autoComplete = <div className="mx_MessageEditor_AutoCompleteWrapper">
<Autocomplete <Autocomplete
ref={this._collectAutocompleteRef} ref={ref => this._autocompleteRef = ref}
query={query} query={query}
onConfirm={this._onAutoCompleteConfirm} onConfirm={this._onAutoCompleteConfirm}
onSelectionChange={this._onAutoCompleteSelectionChange} onSelectionChange={this._onAutoCompleteSelectionChange}
@ -173,18 +160,17 @@ export default class MessageEditor extends React.Component {
return <div className="mx_MessageEditor"> return <div className="mx_MessageEditor">
{ autoComplete } { autoComplete }
<div <div
className="editor" className="mx_MessageEditor_editor"
contentEditable="true" contentEditable="true"
tabIndex="1" tabIndex="1"
onInput={this._onInput} onInput={this._onInput}
onKeyDown={this._onKeyDown} onKeyDown={this._onKeyDown}
ref={this._collectEditorRef} ref={ref => this._editorRef = ref}
></div> ></div>
<div className="buttons"> <div className="mx_MessageEditor_buttons">
<AccessibleButton kind="secondary" onClick={this._onCancelClicked}>{_t("Cancel")}</AccessibleButton> <AccessibleButton kind="secondary" onClick={this._onCancelClicked}>{_t("Cancel")}</AccessibleButton>
<AccessibleButton kind="primary" onClick={this._onSaveClicked}>{_t("Save")}</AccessibleButton> <AccessibleButton kind="primary" onClick={this._onSaveClicked}>{_t("Save")}</AccessibleButton>
</div> </div>
<code className="model"></code>
</div>; </div>;
} }
} }

View file

@ -66,8 +66,6 @@ export default class EditorModel {
} }
_diff(newValue, inputType, caret) { _diff(newValue, inputType, caret) {
// handle deleteContentForward (Delete key)
// and deleteContentBackward (Backspace)
const previousValue = this.parts.reduce((text, p) => text + p.text, ""); const previousValue = this.parts.reduce((text, p) => text + p.text, "");
// can't use caret position with drag and drop // can't use caret position with drag and drop
if (inputType === "deleteByDrag") { if (inputType === "deleteByDrag") {
@ -80,8 +78,6 @@ export default class EditorModel {
update(newValue, inputType, caret) { update(newValue, inputType, caret) {
const diff = this._diff(newValue, inputType, caret); const diff = this._diff(newValue, inputType, caret);
const position = this._positionForOffset(diff.at, caret.atNodeEnd); const position = this._positionForOffset(diff.at, caret.atNodeEnd);
// const valueWithCaret = newValue.slice(0, caret.offset) + "|" + newValue.slice(caret.offset);
// console.log("update at", {diff, valueWithCaret});
let removedOffsetDecrease = 0; let removedOffsetDecrease = 0;
if (diff.removed) { if (diff.removed) {
removedOffsetDecrease = this._removeText(position, diff.removed.length); removedOffsetDecrease = this._removeText(position, diff.removed.length);
@ -93,7 +89,6 @@ export default class EditorModel {
this._mergeAdjacentParts(); this._mergeAdjacentParts();
const caretOffset = diff.at - removedOffsetDecrease + addedLen; const caretOffset = diff.at - removedOffsetDecrease + addedLen;
const newPosition = this._positionForOffset(caretOffset, true); const newPosition = this._positionForOffset(caretOffset, true);
// console.log("caretOffset", {at: diff.at, removedOffsetDecrease, addedLen}, newPosition);
this._setActivePart(newPosition); this._setActivePart(newPosition);
this._updateCallback(newPosition); this._updateCallback(newPosition);
} }
@ -146,12 +141,6 @@ export default class EditorModel {
this._updateCallback(pos); this._updateCallback(pos);
} }
/*
updateCaret(caret) {
// update active part here as well, hiding/showing autocomplete if needed
}
*/
_mergeAdjacentParts(docPos) { _mergeAdjacentParts(docPos) {
let prevPart = this._parts[0]; let prevPart = this._parts[0];
for (let i = 1; i < this._parts.length; ++i) { for (let i = 1; i < this._parts.length; ++i) {

View file

@ -14,22 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export function rerenderModel(editor, model) {
while (editor.firstChild) {
editor.removeChild(editor.firstChild);
}
let lineContainer = document.createElement("div");
editor.appendChild(lineContainer);
for (const part of model.parts) {
if (part.type === "newline") {
lineContainer = document.createElement("div");
editor.appendChild(lineContainer);
} else {
lineContainer.appendChild(part.toDOMNode());
}
}
}
export function renderModel(editor, model) { export function renderModel(editor, model) {
const lines = model.parts.reduce((lines, part) => { const lines = model.parts.reduce((lines, part) => {
if (part.type === "newline") { if (part.type === "newline") {