From 75a2be1a8d2564bbd531652afc17ca7734e842c8 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 23 Apr 2018 01:13:18 +0100 Subject: [PATCH 001/123] WIP (doesn't build yet) replacing draft with slate --- package.json | 7 +- src/ComposerHistoryManager.js | 51 ++++--- .../views/rooms/MessageComposerInput.js | 135 ++++++++++-------- src/stores/MessageComposerStore.js | 20 +-- 4 files changed, 125 insertions(+), 88 deletions(-) diff --git a/package.json b/package.json index 77338b4874..7856304757 100644 --- a/package.json +++ b/package.json @@ -58,9 +58,6 @@ "classnames": "^2.1.2", "commonmark": "^0.28.1", "counterpart": "^0.18.0", - "draft-js": "^0.11.0-alpha", - "draft-js-export-html": "^0.6.0", - "draft-js-export-markdown": "^0.3.0", "emojione": "2.2.7", "file-saver": "^1.3.3", "filesize": "3.5.6", @@ -84,6 +81,10 @@ "react-beautiful-dnd": "^4.0.1", "react-dom": "^15.6.0", "react-gemini-scrollbar": "matrix-org/react-gemini-scrollbar#5e97aef", + "slate": "^0.33.4", + "slate-react": "^0.12.4", + "slate-html-serializer": "^0.6.1", + "slate-md-serializer": "^3.0.3", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index 2757c5bd3d..5c9ae26af0 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -15,38 +15,55 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {ContentState, convertToRaw, convertFromRaw} from 'draft-js'; +import { Value } from 'slate'; +import Html from 'slate-html-serializer'; +import Markdown as Md from 'slate-md-serializer'; +import Plain from 'slate-plain-serializer'; import * as RichText from './RichText'; import Markdown from './Markdown'; + import _clamp from 'lodash/clamp'; -type MessageFormat = 'html' | 'markdown'; +type MessageFormat = 'rich' | 'markdown'; class HistoryItem { // Keeping message for backwards-compatibility message: string; - rawContentState: RawDraftContentState; - format: MessageFormat = 'html'; + value: Value; + format: MessageFormat = 'rich'; - constructor(contentState: ?ContentState, format: ?MessageFormat) { + constructor(value: ?Value, format: ?MessageFormat) { this.rawContentState = contentState ? convertToRaw(contentState) : null; this.format = format; + } - toContentState(outputFormat: MessageFormat): ContentState { - const contentState = convertFromRaw(this.rawContentState); + toValue(outputFormat: MessageFormat): Value { if (outputFormat === 'markdown') { - if (this.format === 'html') { - return ContentState.createFromText(RichText.stateToMarkdown(contentState)); + if (this.format === 'rich') { + // convert a rich formatted history entry to its MD equivalent + const markdown = new Markdown({}); + return new Value({ data: markdown.serialize(value) }); + // return ContentState.createFromText(RichText.stateToMarkdown(contentState)); } - } else { + else if (this.format === 'markdown') { + return value; + } + } else if (outputFormat === 'rich') { if (this.format === 'markdown') { - return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML()); + // convert MD formatted string to its rich equivalent. + const plain = new Plain({}); + const md = new Md({}); + return md.deserialize(plain.serialize(value)); + // return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML()); + } + else if (this.format === 'rich') { + return value; } } - // history item has format === outputFormat - return contentState; + log.error("unknown format -> outputFormat conversion"); + return value; } } @@ -69,16 +86,16 @@ export default class ComposerHistoryManager { this.lastIndex = this.currentIndex; } - save(contentState: ContentState, format: MessageFormat) { - const item = new HistoryItem(contentState, format); + save(value: Value, format: MessageFormat) { + const item = new HistoryItem(value, format); this.history.push(item); this.currentIndex = this.lastIndex + 1; sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item)); } - getItem(offset: number, format: MessageFormat): ?ContentState { + getItem(offset: number, format: MessageFormat): ?Value { this.currentIndex = _clamp(this.currentIndex + offset, 0, this.lastIndex - 1); const item = this.history[this.currentIndex]; - return item ? item.toContentState(format) : null; + return item ? item.toValue(format) : null; } } diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index c142d97b28..1984f72bf9 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -18,9 +18,16 @@ import React from 'react'; import PropTypes from 'prop-types'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; -import {Editor, EditorState, RichUtils, CompositeDecorator, Modifier, - getDefaultKeyBinding, KeyBindingUtil, ContentState, ContentBlock, SelectionState, - Entity} from 'draft-js'; +import { Editor } from 'slate-react'; +import { Value } from 'slate'; + +import Html from 'slate-html-serializer'; +import Markdown as Md from 'slate-md-serializer'; +import Plain from 'slate-plain-serializer'; + +// import {Editor, EditorState, RichUtils, CompositeDecorator, Modifier, +// getDefaultKeyBinding, KeyBindingUtil, ContentState, ContentBlock, SelectionState, +// Entity} from 'draft-js'; import classNames from 'classnames'; import escape from 'lodash/escape'; @@ -61,20 +68,10 @@ const REGEX_EMOJI_WHITESPACE = new RegExp('(?:^|\\s)(' + asciiRegexp + ')\\s$'); const TYPING_USER_TIMEOUT = 10000, TYPING_SERVER_TIMEOUT = 30000; -const ZWS_CODE = 8203; -const ZWS = String.fromCharCode(ZWS_CODE); // zero width space - const ENTITY_TYPES = { AT_ROOM_PILL: 'ATROOMPILL', }; -function stateToMarkdown(state) { - return __stateToMarkdown(state) - .replace( - ZWS, // draft-js-export-markdown adds these - ''); // this is *not* a zero width space, trust me :) -} - function onSendMessageFailed(err, room) { // XXX: temporary logging to try to diagnose // https://github.com/vector-im/riot-web/issues/3148 @@ -103,8 +100,6 @@ export default class MessageComposerInput extends React.Component { }; static getKeyBinding(ev: SyntheticKeyboardEvent): string { - const ctrlCmdOnly = isOnlyCtrlOrCmdKeyEvent(ev); - // Restrict a subset of key bindings to ONLY having ctrl/meta* pressed and // importantly NOT having alt, shift, meta/ctrl* pressed. draft-js does not // handle this in `getDefaultKeyBinding` so we do it ourselves here. @@ -121,7 +116,7 @@ export default class MessageComposerInput extends React.Component { }[ev.keyCode]; if (ctrlCmdCommand) { - if (!ctrlCmdOnly) { + if (!isOnlyCtrlOrCmdKeyEvent(ev)) { return null; } return ctrlCmdCommand; @@ -145,17 +140,6 @@ export default class MessageComposerInput extends React.Component { constructor(props, context) { super(props, context); - this.onAction = this.onAction.bind(this); - this.handleReturn = this.handleReturn.bind(this); - this.handleKeyCommand = this.handleKeyCommand.bind(this); - this.onEditorContentChanged = this.onEditorContentChanged.bind(this); - this.onUpArrow = this.onUpArrow.bind(this); - this.onDownArrow = this.onDownArrow.bind(this); - this.onTab = this.onTab.bind(this); - this.onEscape = this.onEscape.bind(this); - this.setDisplayedCompletion = this.setDisplayedCompletion.bind(this); - this.onMarkdownToggleClicked = this.onMarkdownToggleClicked.bind(this); - this.onTextPasted = this.onTextPasted.bind(this); const isRichtextEnabled = SettingsStore.getValue('MessageComposerInput.isRichTextEnabled'); @@ -185,6 +169,7 @@ export default class MessageComposerInput extends React.Component { this.client = MatrixClientPeg.get(); } +/* findPillEntities(contentState: ContentState, contentBlock: ContentBlock, callback) { contentBlock.findEntityRanges( (character) => { @@ -199,13 +184,15 @@ export default class MessageComposerInput extends React.Component { }, callback, ); } +*/ /* - * "Does the right thing" to create an EditorState, based on: + * "Does the right thing" to create an Editor value, based on: * - whether we've got rich text mode enabled * - contentState was passed in */ - createEditorState(richText: boolean, contentState: ?ContentState): EditorState { + createEditorState(richText: boolean, value: ?Value): Value { +/* const decorators = richText ? RichText.getScopedRTDecorators(this.props) : RichText.getScopedMDDecorators(this.props); const shouldShowPillAvatar = !SettingsStore.getValue("Pill.shouldHidePillAvatar"); @@ -239,7 +226,6 @@ export default class MessageComposerInput extends React.Component { }, }); const compositeDecorator = new CompositeDecorator(decorators); - let editorState = null; if (contentState) { editorState = EditorState.createWithContent(contentState, compositeDecorator); @@ -248,6 +234,8 @@ export default class MessageComposerInput extends React.Component { } return EditorState.moveFocusToEnd(editorState); +*/ + return value; } componentDidMount() { @@ -260,12 +248,14 @@ export default class MessageComposerInput extends React.Component { } componentWillUpdate(nextProps, nextState) { +/* // this is dirty, but moving all this state to MessageComposer is dirtier if (this.props.onInputStateChanged && nextState !== this.state) { const state = this.getSelectionInfo(nextState.editorState); state.isRichtextEnabled = nextState.isRichtextEnabled; this.props.onInputStateChanged(state); } +*/ } onAction = (payload) => { @@ -277,6 +267,7 @@ export default class MessageComposerInput extends React.Component { case 'focus_composer': editor.focus(); break; +/* case 'insert_mention': { // Pretend that we've autocompleted this user because keeping two code // paths for inserting a user pill is not fun @@ -322,6 +313,7 @@ export default class MessageComposerInput extends React.Component { } } break; +*/ } }; @@ -372,7 +364,7 @@ export default class MessageComposerInput extends React.Component { stopServerTypingTimer() { if (this.serverTypingTimer) { - clearTimeout(this.servrTypingTimer); + clearTimeout(this.serverTypingTimer); this.serverTypingTimer = null; } } @@ -492,9 +484,9 @@ export default class MessageComposerInput extends React.Component { // Record the editor state for this room so that it can be retrieved after // switching to another room and back dis.dispatch({ - action: 'content_state', + action: 'editor_state', room_id: this.props.room.roomId, - content_state: state.editorState.getCurrentContent(), + editor_state: state.editorState.getCurrentContent(), }); if (!state.hasOwnProperty('originalEditorState')) { @@ -528,28 +520,36 @@ export default class MessageComposerInput extends React.Component { enableRichtext(enabled: boolean) { if (enabled === this.state.isRichtextEnabled) return; - let contentState = null; + // FIXME: this conversion should be handled in the store, surely + // i.e. "convert my current composer value into Rich or MD, as ComposerHistoryManager already does" + + let value = null; if (enabled) { - const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText()); - contentState = RichText.htmlToContentState(md.toHTML()); + // const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText()); + // contentState = RichText.htmlToContentState(md.toHTML()); + + const plain = new Plain({}); + const md = new Md({}); + value = md.deserialize(plain.serialize(this.state.editorState)); } else { - let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); - if (markdown[markdown.length - 1] === '\n') { - markdown = markdown.substring(0, markdown.length - 1); // stateToMarkdown tacks on an extra newline (?!?) - } - contentState = ContentState.createFromText(markdown); + // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); + // value = ContentState.createFromText(markdown); + + const markdown = new Markdown({}); + value = Value({ data: markdown.serialize(value) }); } Analytics.setRichtextMode(enabled); this.setState({ - editorState: this.createEditorState(enabled, contentState), + editorState: this.createEditorState(enabled, value), isRichtextEnabled: enabled, }); SettingsStore.setValue("MessageComposerInput.isRichTextEnabled", null, SettingLevel.ACCOUNT, enabled); } handleKeyCommand = (command: string): boolean => { +/* if (command === 'toggle-mode') { this.enableRichtext(!this.state.isRichtextEnabled); return true; @@ -658,11 +658,11 @@ export default class MessageComposerInput extends React.Component { this.setState({editorState: newState}); return true; } - +*/ return false; }; - - onTextPasted(text: string, html?: string) { +/* + onTextPasted = (text: string, html?: string) => { const currentSelection = this.state.editorState.getSelection(); const currentContent = this.state.editorState.getCurrentContent(); @@ -682,9 +682,10 @@ export default class MessageComposerInput extends React.Component { newEditorState = EditorState.forceSelection(newEditorState, contentState.getSelectionAfter()); this.onEditorContentChanged(newEditorState); return true; - } - - handleReturn(ev) { + }; +*/ + handleReturn = (ev) => { +/* if (ev.shiftKey) { this.onEditorContentChanged(RichUtils.insertSoftNewline(this.state.editorState)); return true; @@ -701,15 +702,21 @@ export default class MessageComposerInput extends React.Component { // See handleKeyCommand (when command === 'backspace') return false; } - - const contentState = this.state.editorState.getCurrentContent(); +*/ + const contentState = this.state.editorState; +/* if (!contentState.hasText()) { return true; } +*/ + const plain = new Plain({}); + value = md.deserialize(); - let contentText = contentState.getPlainText(), contentHTML; + let contentText = plain.serialize(contentState); + let contentHTML; +/* // Strip MD user (tab-completed) mentions to preserve plaintext mention behaviour. // We have to do this now as opposed to after calculating the contentText for MD // mode because entity positions may not be maintained when using @@ -720,10 +727,12 @@ export default class MessageComposerInput extends React.Component { // Some commands (/join) require pills to be replaced with their text content const commandText = this.removeMDLinks(contentState, ['#']); +*/ + const commandText = contentText; const cmd = SlashCommands.processInput(this.props.room.roomId, commandText); if (cmd) { if (!cmd.error) { - this.historyManager.save(contentState, this.state.isRichtextEnabled ? 'html' : 'markdown'); + this.historyManager.save(contentState, this.state.isRichtextEnabled ? 'rich' : 'markdown'); this.setState({ editorState: this.createEditorState(), }); @@ -754,6 +763,7 @@ export default class MessageComposerInput extends React.Component { const quotingEv = RoomViewStore.getQuotingEvent(); if (this.state.isRichtextEnabled) { +/* // We should only send HTML if any block is styled or contains inline style let shouldSendHTML = false; @@ -788,6 +798,8 @@ export default class MessageComposerInput extends React.Component { }); shouldSendHTML = hasLink; } +*/ + let shouldSendHTML = true; if (shouldSendHTML) { contentHTML = HtmlUtils.processHtmlForSending( RichText.contentStateToHTML(contentState), @@ -797,6 +809,7 @@ export default class MessageComposerInput extends React.Component { // Use the original contentState because `contentText` has had mentions // stripped and these need to end up in contentHTML. +/* // Replace all Entities of type `LINK` with markdown link equivalents. // TODO: move this into `Markdown` and do the same conversion in the other // two places (toggling from MD->RT mode and loading MD history into RT mode) @@ -817,7 +830,7 @@ export default class MessageComposerInput extends React.Component { }); return blockText; }).join('\n'); - +*/ const md = new Markdown(pt); // if contains no HTML and we're not quoting (needing HTML) if (md.isPlainText() && !quotingEv) { @@ -832,7 +845,7 @@ export default class MessageComposerInput extends React.Component { this.historyManager.save( contentState, - this.state.isRichtextEnabled ? 'html' : 'markdown', + this.state.isRichtextEnabled ? 'rich' : 'markdown', ); if (contentText.startsWith('/me')) { @@ -881,7 +894,7 @@ export default class MessageComposerInput extends React.Component { }); return true; - } + }; onUpArrow = (e) => { this.onVerticalArrow(e, true); @@ -896,6 +909,7 @@ export default class MessageComposerInput extends React.Component { return; } +/* // Select history only if we are not currently auto-completing if (this.autocomplete.state.completionList.length === 0) { // Don't go back in history if we're in the middle of a multi-line message @@ -927,8 +941,10 @@ export default class MessageComposerInput extends React.Component { this.moveAutocompleteSelection(up); e.preventDefault(); } +*/ }; +/* selectHistory = async (up) => { const delta = up ? -1 : 1; @@ -950,7 +966,7 @@ export default class MessageComposerInput extends React.Component { return; } - const newContent = this.historyManager.getItem(delta, this.state.isRichtextEnabled ? 'html' : 'markdown'); + const newContent = this.historyManager.getItem(delta, this.state.isRichtextEnabled ? 'rich' : 'markdown'); if (!newContent) return false; let editorState = EditorState.push( this.state.editorState, @@ -969,6 +985,7 @@ export default class MessageComposerInput extends React.Component { this.setState({editorState}); return true; }; +*/ onTab = async (e) => { this.setState({ @@ -1061,8 +1078,9 @@ export default class MessageComposerInput extends React.Component { return true; }; - onFormatButtonClicked(name: "bold" | "italic" | "strike" | "code" | "underline" | "quote" | "bullet" | "numbullet", e) { + onFormatButtonClicked = (name: "bold" | "italic" | "strike" | "code" | "underline" | "quote" | "bullet" | "numbullet", e) => { e.preventDefault(); // don't steal focus from the editor! +/* const command = { code: 'code-block', quote: 'blockquote', @@ -1070,7 +1088,8 @@ export default class MessageComposerInput extends React.Component { numbullet: 'ordered-list-item', }[name] || name; this.handleKeyCommand(command); - } +*/ + }; /* returns inline style and block type of current SelectionState so MessageComposer can render formatting buttons. */ diff --git a/src/stores/MessageComposerStore.js b/src/stores/MessageComposerStore.js index d02bcf953f..3b1ab1fa72 100644 --- a/src/stores/MessageComposerStore.js +++ b/src/stores/MessageComposerStore.js @@ -14,16 +14,17 @@ See the License for the specific language governing permissions and limitations under the License. */ import dis from '../dispatcher'; -import {Store} from 'flux/utils'; -import {convertToRaw, convertFromRaw} from 'draft-js'; +import { Store } from 'flux/utils'; const INITIAL_STATE = { - editorStateMap: localStorage.getItem('content_state') ? - JSON.parse(localStorage.getItem('content_state')) : {}, + // a map of room_id to rich text editor composer state + editorStateMap: localStorage.getItem('editor_state') ? + JSON.parse(localStorage.getItem('editor_state')) : {}, }; /** - * A class for storing application state to do with the message composer. This is a simple + * A class for storing application state to do with the message composer (specifically + * in-progress message drafts). This is a simple * flux store that listens for actions and updates its state accordingly, informing any * listeners (views) of state changes. */ @@ -42,7 +43,7 @@ class MessageComposerStore extends Store { __onDispatch(payload) { switch (payload.action) { - case 'content_state': + case 'editor_state': this._contentState(payload); break; case 'on_logged_out': @@ -53,16 +54,15 @@ class MessageComposerStore extends Store { _contentState(payload) { const editorStateMap = this._state.editorStateMap; - editorStateMap[payload.room_id] = convertToRaw(payload.content_state); - localStorage.setItem('content_state', JSON.stringify(editorStateMap)); + editorStateMap[payload.room_id] = payload.editor_state; + localStorage.setItem('editor_state', JSON.stringify(editorStateMap)); this._setState({ editorStateMap: editorStateMap, }); } getContentState(roomId) { - return this._state.editorStateMap[roomId] ? - convertFromRaw(this._state.editorStateMap[roomId]) : null; + return this._state.editorStateMap[roomId]; } reset() { From e62e43def6401a0c9c518308418f5a90fc2b10b1 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 5 May 2018 23:25:04 +0100 Subject: [PATCH 002/123] comment out more draft stuff --- src/ComposerHistoryManager.js | 2 +- src/components/views/avatars/RoomAvatar.js | 2 +- .../views/rooms/MessageComposerInput.js | 36 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index 5c9ae26af0..938be9eee4 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -17,7 +17,7 @@ limitations under the License. import { Value } from 'slate'; import Html from 'slate-html-serializer'; -import Markdown as Md from 'slate-md-serializer'; +import { Markdown as Md } from 'slate-md-serializer'; import Plain from 'slate-plain-serializer'; import * as RichText from './RichText'; import Markdown from './Markdown'; diff --git a/src/components/views/avatars/RoomAvatar.js b/src/components/views/avatars/RoomAvatar.js index 499e575227..e37d8d5d19 100644 --- a/src/components/views/avatars/RoomAvatar.js +++ b/src/components/views/avatars/RoomAvatar.js @@ -177,7 +177,7 @@ module.exports = React.createClass({ render: function() { const BaseAvatar = sdk.getComponent("avatars.BaseAvatar"); - const {room, oobData, ...otherProps} = this.props; + const {room, oobData, viewAvatarOnClick, ...otherProps} = this.props; const roomName = room ? room.name : oobData.name; diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index c8523acea1..af942a75e6 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -22,7 +22,7 @@ import { Editor } from 'slate-react'; import { Value } from 'slate'; import Html from 'slate-html-serializer'; -import Markdown as Md from 'slate-md-serializer'; +import { Markdown as Md } from 'slate-md-serializer'; import Plain from 'slate-plain-serializer'; // import {Editor, EditorState, RichUtils, CompositeDecorator, Modifier, @@ -1103,6 +1103,10 @@ export default class MessageComposerInput extends React.Component { /* returns inline style and block type of current SelectionState so MessageComposer can render formatting buttons. */ getSelectionInfo(editorState: EditorState) { + return { + [], null + }; +/* const styleName = { BOLD: _td('bold'), ITALIC: _td('italic'), @@ -1130,13 +1134,17 @@ export default class MessageComposerInput extends React.Component { style, blockType, }; +*/ } getAutocompleteQuery(contentState: ContentState) { + return []; + // Don't send markdown links to the autocompleter - return this.removeMDLinks(contentState, ['@', '#']); + // return this.removeMDLinks(contentState, ['@', '#']); } +/* removeMDLinks(contentState: ContentState, prefixes: string[]) { const plaintext = contentState.getPlainText(); if (!plaintext) return ''; @@ -1169,7 +1177,7 @@ export default class MessageComposerInput extends React.Component { } }); } - +*/ onMarkdownToggleClicked = (e) => { e.preventDefault(); // don't steal focus from the editor! this.handleKeyCommand('toggle-mode'); @@ -1178,25 +1186,14 @@ export default class MessageComposerInput extends React.Component { render() { const activeEditorState = this.state.originalEditorState || this.state.editorState; - // From https://github.com/facebook/draft-js/blob/master/examples/rich/rich.html#L92 - // If the user changes block type before entering any text, we can - // either style the placeholder or hide it. - let hidePlaceholder = false; - const contentState = activeEditorState.getCurrentContent(); - if (!contentState.hasText()) { - if (contentState.getBlockMap().first().getType() !== 'unstyled') { - hidePlaceholder = true; - } - } - const className = classNames('mx_MessageComposer_input', { mx_MessageComposer_input_empty: hidePlaceholder, mx_MessageComposer_input_error: this.state.someCompletions === false, }); - const content = activeEditorState.getCurrentContent(); - const selection = RichText.selectionStateToTextOffsets(activeEditorState.getSelection(), - activeEditorState.getCurrentContent().getBlocksAsArray()); + // const content = activeEditorState.getCurrentContent(); + // const selection = RichText.selectionStateToTextOffsets(activeEditorState.getSelection(), + // activeEditorState.getCurrentContent().getBlocksAsArray()); return (
@@ -1219,6 +1216,7 @@ export default class MessageComposerInput extends React.Component { + spellCheck={true} + */ + />
); From f4ed820b6f0d8bb4d0aa6441c7633a334d47afcc Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 5 May 2018 23:38:14 +0100 Subject: [PATCH 003/123] fix stubbing --- src/components/views/rooms/MessageComposerInput.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index af942a75e6..6290a5c15d 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1103,9 +1103,7 @@ export default class MessageComposerInput extends React.Component { /* returns inline style and block type of current SelectionState so MessageComposer can render formatting buttons. */ getSelectionInfo(editorState: EditorState) { - return { - [], null - }; + return {}; /* const styleName = { BOLD: _td('bold'), From 05eba3fa32703b075c9012f125b9a79cf135e038 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 6 May 2018 00:18:11 +0100 Subject: [PATCH 004/123] stub out more until it loads... --- .../views/rooms/MessageComposerInput.js | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 6290a5c15d..e7cbb1abde 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -237,7 +237,13 @@ export default class MessageComposerInput extends React.Component { return EditorState.moveFocusToEnd(editorState); */ - return value; + if (value) { + // create with this value + } + else { + value = Value.create(); + } + return value; } componentDidMount() { @@ -262,7 +268,7 @@ export default class MessageComposerInput extends React.Component { onAction = (payload) => { const editor = this.refs.editor; - let contentState = this.state.editorState.getCurrentContent(); + let editorState = this.state.editorState; switch (payload.action) { case 'reply_to_event': @@ -1030,6 +1036,7 @@ export default class MessageComposerInput extends React.Component { * If passed a non-null displayedCompletion, modifies state.originalEditorState to compute new state.editorState. */ setDisplayedCompletion = async (displayedCompletion: ?Completion): boolean => { +/* const activeEditorState = this.state.originalEditorState || this.state.editorState; if (displayedCompletion == null) { @@ -1084,6 +1091,7 @@ export default class MessageComposerInput extends React.Component { // for some reason, doing this right away does not update the editor :( // setTimeout(() => this.refs.editor.focus(), 50); +*/ return true; }; @@ -1102,7 +1110,7 @@ export default class MessageComposerInput extends React.Component { /* returns inline style and block type of current SelectionState so MessageComposer can render formatting buttons. */ - getSelectionInfo(editorState: EditorState) { + getSelectionInfo(editorState: Value) { return {}; /* const styleName = { @@ -1136,7 +1144,7 @@ export default class MessageComposerInput extends React.Component { } getAutocompleteQuery(contentState: ContentState) { - return []; + return ''; // Don't send markdown links to the autocompleter // return this.removeMDLinks(contentState, ['@', '#']); @@ -1184,11 +1192,20 @@ export default class MessageComposerInput extends React.Component { render() { const activeEditorState = this.state.originalEditorState || this.state.editorState; + let hidePlaceholder = false; + // FIXME: in case we need to implement manual placeholdering + const className = classNames('mx_MessageComposer_input', { mx_MessageComposer_input_empty: hidePlaceholder, mx_MessageComposer_input_error: this.state.someCompletions === false, }); + const content = null; + const selection = { + start: 0, + end: 0, + }; + // const content = activeEditorState.getCurrentContent(); // const selection = RichText.selectionStateToTextOffsets(activeEditorState.getSelection(), // activeEditorState.getCurrentContent().getBlocksAsArray()); @@ -1214,6 +1231,7 @@ export default class MessageComposerInput extends React.Component { Date: Sun, 6 May 2018 01:18:26 +0100 Subject: [PATCH 005/123] stub out yet more --- res/css/views/rooms/_MessageComposer.scss | 8 ++++++ .../views/rooms/MessageComposerInput.js | 26 ++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/res/css/views/rooms/_MessageComposer.scss b/res/css/views/rooms/_MessageComposer.scss index 2e8f07b7ef..531c4442c1 100644 --- a/res/css/views/rooms/_MessageComposer.scss +++ b/res/css/views/rooms/_MessageComposer.scss @@ -84,6 +84,14 @@ limitations under the License. margin-right: 6px; } +.mx_MessageComposer_editor { + width: 100%; + flex: 1; + max-height: 120px; + min-height: 21px; + overflow: auto; +} + @keyframes visualbell { from { background-color: #faa } diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index e7cbb1abde..e560ddb5c7 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -19,7 +19,7 @@ import PropTypes from 'prop-types'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import { Editor } from 'slate-react'; -import { Value } from 'slate'; +import { Value, Document } from 'slate'; import Html from 'slate-html-serializer'; import { Markdown as Md } from 'slate-md-serializer'; @@ -238,12 +238,17 @@ export default class MessageComposerInput extends React.Component { return EditorState.moveFocusToEnd(editorState); */ if (value) { - // create with this value + // create from the existing value... } else { - value = Value.create(); + // ...or create a new one. } - return value; + + return Value.create({ + document: Document.create({ + nodes: [], + }), + }); } componentDidMount() { @@ -394,6 +399,7 @@ export default class MessageComposerInput extends React.Component { // Called by Draft to change editor contents onEditorContentChanged = (editorState: EditorState) => { +/* editorState = RichText.attachImmutableEntitiesToEmoji(editorState); const currentBlock = editorState.getSelection().getStartKey(); @@ -449,7 +455,7 @@ export default class MessageComposerInput extends React.Component { editorState = EditorState.forceSelection(editorState, newContentState.getSelectionAfter()); } } - +*/ /* Since a modification was made, set originalEditorState to null, since newState is now our original */ this.setState({ editorState, @@ -466,6 +472,7 @@ export default class MessageComposerInput extends React.Component { * @param callback */ setState(state, callback) { +/* if (state.editorState != null) { state.editorState = RichText.attachImmutableEntitiesToEmoji( state.editorState); @@ -501,12 +508,12 @@ export default class MessageComposerInput extends React.Component { state.originalEditorState = null; } } - +*/ super.setState(state, () => { if (callback != null) { callback(); } - +/* const textContent = this.state.editorState.getCurrentContent().getPlainText(); const selection = RichText.selectionStateToTextOffsets( this.state.editorState.getSelection(), @@ -522,6 +529,7 @@ export default class MessageComposerInput extends React.Component { let editorRoot = this.refs.editor.refs.editor.parentNode.parentNode; editorRoot.scrollTop = editorRoot.scrollHeight; } +*/ }); } @@ -1230,11 +1238,11 @@ export default class MessageComposerInput extends React.Component { src={`img/button-md-${!this.state.isRichtextEnabled}.png`} /> Date: Sun, 6 May 2018 15:27:27 +0100 Subject: [PATCH 006/123] make slate actually work as a textarea --- .../views/rooms/MessageComposerInput.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index e560ddb5c7..9a0863810e 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -237,18 +237,13 @@ export default class MessageComposerInput extends React.Component { return EditorState.moveFocusToEnd(editorState); */ - if (value) { - // create from the existing value... + if (value instanceof Value) { + return value; } else { // ...or create a new one. + return Plain.deserialize('') } - - return Value.create({ - document: Document.create({ - nodes: [], - }), - }); } componentDidMount() { @@ -398,7 +393,7 @@ export default class MessageComposerInput extends React.Component { } // Called by Draft to change editor contents - onEditorContentChanged = (editorState: EditorState) => { + onEditorContentChanged = (change: Change) => { /* editorState = RichText.attachImmutableEntitiesToEmoji(editorState); @@ -458,7 +453,7 @@ export default class MessageComposerInput extends React.Component { */ /* Since a modification was made, set originalEditorState to null, since newState is now our original */ this.setState({ - editorState, + editorState: change.value, originalEditorState: null, }); }; From ff42ef4a58baf57580cd504b103b474a46dbb4cf Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 6 May 2018 22:08:36 +0100 Subject: [PATCH 007/123] make it work for MD mode (modulo history) --- src/ComposerHistoryManager.js | 10 ++-- src/RichText.js | 48 +++++++++++++------ .../views/rooms/MessageComposerInput.js | 31 ++++++------ 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index 938be9eee4..e52a8a677f 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -34,17 +34,15 @@ class HistoryItem { format: MessageFormat = 'rich'; constructor(value: ?Value, format: ?MessageFormat) { - this.rawContentState = contentState ? convertToRaw(contentState) : null; + this.value = value; this.format = format; - } toValue(outputFormat: MessageFormat): Value { if (outputFormat === 'markdown') { if (this.format === 'rich') { // convert a rich formatted history entry to its MD equivalent - const markdown = new Markdown({}); - return new Value({ data: markdown.serialize(value) }); + return Plain.deserialize(Md.serialize(value)); // return ContentState.createFromText(RichText.stateToMarkdown(contentState)); } else if (this.format === 'markdown') { @@ -53,9 +51,7 @@ class HistoryItem { } else if (outputFormat === 'rich') { if (this.format === 'markdown') { // convert MD formatted string to its rich equivalent. - const plain = new Plain({}); - const md = new Md({}); - return md.deserialize(plain.serialize(value)); + return Md.deserialize(Plain.serialize(value)); // return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML()); } else if (this.format === 'rich') { diff --git a/src/RichText.js b/src/RichText.js index 12274ee9f3..7ffb4dd785 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -1,4 +1,24 @@ +/* +Copyright 2015 - 2017 OpenMarket Ltd +Copyright 2017 Vector Creations Ltd +Copyright 2018 New Vector Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + import React from 'react'; + +/* import { Editor, EditorState, @@ -12,11 +32,15 @@ import { SelectionState, Entity, } from 'draft-js'; +import { stateToMarkdown as __stateToMarkdown } from 'draft-js-export-markdown'; +*/ + +import Html from 'slate-html-serializer'; + import * as sdk from './index'; import * as emojione from 'emojione'; -import {stateToHTML} from 'draft-js-export-html'; -import {SelectionRange} from "./autocomplete/Autocompleter"; -import {stateToMarkdown as __stateToMarkdown} from 'draft-js-export-markdown'; + +import { SelectionRange } from "./autocomplete/Autocompleter"; const MARKDOWN_REGEX = { LINK: /(?:\[([^\]]+)\]\(([^\)]+)\))|\<(\w+:\/\/[^\>]+)\>/g, @@ -33,6 +57,7 @@ const EMOJI_REGEX = new RegExp(emojione.unicodeRegexp, 'g'); const ZWS_CODE = 8203; const ZWS = String.fromCharCode(ZWS_CODE); // zero width space + export function stateToMarkdown(state) { return __stateToMarkdown(state) .replace( @@ -40,19 +65,12 @@ export function stateToMarkdown(state) { ''); // this is *not* a zero width space, trust me :) } -export const contentStateToHTML = (contentState: ContentState) => { - return stateToHTML(contentState, { - inlineStyles: { - UNDERLINE: { - element: 'u', - }, - }, - }); -}; +export const editorStateToHTML = (editorState: Value) => { + return Html.deserialize(editorState); +} -export function htmlToContentState(html: string): ContentState { - const blockArray = convertFromHTML(html).contentBlocks; - return ContentState.createFromBlockArray(blockArray); +export function htmlToEditorState(html: string): Value { + return Html.serialize(html); } function unicodeToEmojiUri(str) { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 9a0863810e..53f7a6d474 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -19,7 +19,7 @@ import PropTypes from 'prop-types'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import { Editor } from 'slate-react'; -import { Value, Document } from 'slate'; +import { Value, Document, Event } from 'slate'; import Html from 'slate-html-serializer'; import { Markdown as Md } from 'slate-md-serializer'; @@ -539,15 +539,12 @@ export default class MessageComposerInput extends React.Component { // const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText()); // contentState = RichText.htmlToContentState(md.toHTML()); - const plain = new Plain({}); - const md = new Md({}); - value = md.deserialize(plain.serialize(this.state.editorState)); + value = Md.deserialize(Plain.serialize(this.state.editorState)); } else { // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); // value = ContentState.createFromText(markdown); - const markdown = new Markdown({}); - value = Value({ data: markdown.serialize(value) }); + value = Plain.deserialize(Md.serialize(this.state.editorState)); } Analytics.setRichtextMode(enabled); @@ -559,6 +556,12 @@ export default class MessageComposerInput extends React.Component { SettingsStore.setValue("MessageComposerInput.isRichTextEnabled", null, SettingLevel.ACCOUNT, enabled); } + onKeyDown = (ev: Event, change: Change, editor: Editor) => { + if (ev.keyCode === KeyCode.ENTER) { + return this.handleReturn(ev); + } + } + handleKeyCommand = (command: string): boolean => { /* if (command === 'toggle-mode') { @@ -721,10 +724,7 @@ export default class MessageComposerInput extends React.Component { } */ - const plain = new Plain({}); - value = md.deserialize(); - - let contentText = plain.serialize(contentState); + let contentText = Plain.serialize(contentState); let contentHTML; /* @@ -808,10 +808,10 @@ export default class MessageComposerInput extends React.Component { shouldSendHTML = hasLink; } */ - let shouldSendHTML = true; + let shouldSendHTML = true; if (shouldSendHTML) { contentHTML = HtmlUtils.processHtmlForSending( - RichText.contentStateToHTML(contentState), + RichText.editorStateToHTML(contentState), ); } } else { @@ -840,11 +840,12 @@ export default class MessageComposerInput extends React.Component { return blockText; }).join('\n'); */ - const md = new Markdown(pt); + const md = new Markdown(contentText); // if contains no HTML and we're not quoting (needing HTML) if (md.isPlainText() && !mustSendHTML) { contentText = md.toPlaintext(); } else { + contentText = md.toPlaintext(); contentHTML = md.toHTML(); } } @@ -898,7 +899,6 @@ export default class MessageComposerInput extends React.Component { }); } - this.client.sendMessage(this.props.room.roomId, content).then((res) => { dis.dispatch({ action: 'message_sent', @@ -909,7 +909,7 @@ export default class MessageComposerInput extends React.Component { this.setState({ editorState: this.createEditorState(), - }); + }, ()=>{ this.refs.editor.focus() }); return true; }; @@ -1237,6 +1237,7 @@ export default class MessageComposerInput extends React.Component { placeholder={this.props.placeholder} value={this.state.editorState} onChange={this.onEditorContentChanged} + onKeyDown={this.onKeyDown} /* blockStyleFn={MessageComposerInput.getBlockStyle} keyBindingFn={MessageComposerInput.getKeyBinding} From 8b2eb2c4003acc63af3689e9ff7e4f235b32ed39 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Tue, 8 May 2018 01:54:06 +0100 Subject: [PATCH 008/123] make history work again --- res/css/views/rooms/_MessageComposer.scss | 7 +- src/ComposerHistoryManager.js | 30 +++-- .../views/rooms/MessageComposerInput.js | 108 +++++++++--------- 3 files changed, 78 insertions(+), 67 deletions(-) diff --git a/res/css/views/rooms/_MessageComposer.scss b/res/css/views/rooms/_MessageComposer.scss index 531c4442c1..a11ebeff7b 100644 --- a/res/css/views/rooms/_MessageComposer.scss +++ b/res/css/views/rooms/_MessageComposer.scss @@ -78,7 +78,7 @@ limitations under the License. display: flex; flex-direction: column; min-height: 60px; - justify-content: center; + justify-content: start; align-items: flex-start; font-size: 14px; margin-right: 6px; @@ -86,9 +86,8 @@ limitations under the License. .mx_MessageComposer_editor { width: 100%; - flex: 1; max-height: 120px; - min-height: 21px; + min-height: 19px; overflow: auto; } @@ -106,6 +105,7 @@ limitations under the License. display: none; } +/* .mx_MessageComposer_input .DraftEditor-root { width: 100%; flex: 1; @@ -114,6 +114,7 @@ limitations under the License. min-height: 21px; overflow: auto; } +*/ .mx_MessageComposer_input .DraftEditor-root .DraftEditor-editorContainer { /* Ensure mx_UserPill and mx_RoomPill (see _RichText) are not obscured from the top */ diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index e52a8a677f..9a6970a376 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -38,28 +38,44 @@ class HistoryItem { this.format = format; } + static fromJSON(obj): HistoryItem { + return new HistoryItem( + Value.fromJSON(obj.value), + obj.format + ); + } + + toJSON(): Object { + return { + value: this.value.toJSON(), + format: this.format + }; + } + + // FIXME: rather than supporting storing history in either format, why don't we pick + // one canonical form? toValue(outputFormat: MessageFormat): Value { if (outputFormat === 'markdown') { if (this.format === 'rich') { // convert a rich formatted history entry to its MD equivalent - return Plain.deserialize(Md.serialize(value)); + return Plain.deserialize(Md.serialize(this.value)); // return ContentState.createFromText(RichText.stateToMarkdown(contentState)); } else if (this.format === 'markdown') { - return value; + return this.value; } } else if (outputFormat === 'rich') { if (this.format === 'markdown') { // convert MD formatted string to its rich equivalent. - return Md.deserialize(Plain.serialize(value)); + return Md.deserialize(Plain.serialize(this.value)); // return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML()); } else if (this.format === 'rich') { - return value; + return this.value; } } log.error("unknown format -> outputFormat conversion"); - return value; + return this.value; } } @@ -76,7 +92,7 @@ export default class ComposerHistoryManager { let item; for (; item = sessionStorage.getItem(`${this.prefix}[${this.currentIndex}]`); this.currentIndex++) { this.history.push( - Object.assign(new HistoryItem(), JSON.parse(item)), + HistoryItem.fromJSON(JSON.parse(item)) ); } this.lastIndex = this.currentIndex; @@ -86,7 +102,7 @@ export default class ComposerHistoryManager { const item = new HistoryItem(value, format); this.history.push(item); this.currentIndex = this.lastIndex + 1; - sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item)); + sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item.toJSON())); } getItem(offset: number, format: MessageFormat): ?Value { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 53f7a6d474..4b950c429d 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -15,6 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ import React from 'react'; +import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; @@ -101,6 +102,7 @@ export default class MessageComposerInput extends React.Component { onInputStateChanged: PropTypes.func, }; +/* static getKeyBinding(ev: SyntheticKeyboardEvent): string { // Restrict a subset of key bindings to ONLY having ctrl/meta* pressed and // importantly NOT having alt, shift, meta/ctrl* pressed. draft-js does not @@ -135,6 +137,7 @@ export default class MessageComposerInput extends React.Component { return null; } +*/ client: MatrixClient; autocomplete: Autocomplete; @@ -392,8 +395,7 @@ export default class MessageComposerInput extends React.Component { } } - // Called by Draft to change editor contents - onEditorContentChanged = (change: Change) => { + onChange = (change: Change) => { /* editorState = RichText.attachImmutableEntitiesToEmoji(editorState); @@ -557,17 +559,25 @@ export default class MessageComposerInput extends React.Component { } onKeyDown = (ev: Event, change: Change, editor: Editor) => { - if (ev.keyCode === KeyCode.ENTER) { - return this.handleReturn(ev); + switch (ev.keyCode) { + case KeyCode.ENTER: + return this.handleReturn(ev); + case KeyCode.UP: + return this.onVerticalArrow(ev, true); + case KeyCode.DOWN: + return this.onVerticalArrow(ev, false); + default: + // don't intercept it + return; } } handleKeyCommand = (command: string): boolean => { -/* if (command === 'toggle-mode') { this.enableRichtext(!this.state.isRichtextEnabled); return true; } +/* let newState: ?EditorState = null; // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown. @@ -699,12 +709,10 @@ export default class MessageComposerInput extends React.Component { }; */ handleReturn = (ev) => { -/* if (ev.shiftKey) { - this.onEditorContentChanged(RichUtils.insertSoftNewline(this.state.editorState)); - return true; + return; } - +/* const currentBlockType = RichUtils.getCurrentBlockType(this.state.editorState); if ( ['code-block', 'blockquote', 'unordered-list-item', 'ordered-list-item'] @@ -718,15 +726,12 @@ export default class MessageComposerInput extends React.Component { } */ const contentState = this.state.editorState; -/* - if (!contentState.hasText()) { - return true; - } -*/ let contentText = Plain.serialize(contentState); let contentHTML; + if (contentText === '') return true; + /* // Strip MD user (tab-completed) mentions to preserve plaintext mention behaviour. // We have to do this now as opposed to after calculating the contentText for MD @@ -914,41 +919,39 @@ export default class MessageComposerInput extends React.Component { return true; }; - onUpArrow = (e) => { - this.onVerticalArrow(e, true); - }; - - onDownArrow = (e) => { - this.onVerticalArrow(e, false); - }; - onVerticalArrow = (e, up) => { if (e.ctrlKey || e.shiftKey || e.altKey || e.metaKey) { return; } -/* // Select history only if we are not currently auto-completing if (this.autocomplete.state.completionList.length === 0) { - // Don't go back in history if we're in the middle of a multi-line message - const selection = this.state.editorState.getSelection(); - const blockKey = selection.getStartKey(); - const firstBlock = this.state.editorState.getCurrentContent().getFirstBlock(); - const lastBlock = this.state.editorState.getCurrentContent().getLastBlock(); - let canMoveUp = false; - let canMoveDown = false; - if (blockKey === firstBlock.getKey()) { - canMoveUp = selection.getStartOffset() === selection.getEndOffset() && - selection.getStartOffset() === 0; + // determine whether our cursor is at the top or bottom of the multiline + // input box by just looking at the position of the plain old DOM selection. + const selection = window.getSelection(); + const range = selection.getRangeAt(0); + const cursorRect = range.getBoundingClientRect(); + + const editorNode = ReactDOM.findDOMNode(this.refs.editor); + const editorRect = editorNode.getBoundingClientRect(); + + let navigateHistory = false; + if (up) { + let scrollCorrection = editorNode.scrollTop; + if (cursorRect.top - editorRect.top + scrollCorrection == 0) { + navigateHistory = true; + } + } + else { + let scrollCorrection = + editorNode.scrollHeight - editorNode.clientHeight - editorNode.scrollTop; + if (cursorRect.bottom - editorRect.bottom + scrollCorrection == 0) { + navigateHistory = true; + } } - if (blockKey === lastBlock.getKey()) { - canMoveDown = selection.getStartOffset() === selection.getEndOffset() && - selection.getStartOffset() === lastBlock.getText().length; - } - - if ((up && !canMoveUp) || (!up && !canMoveDown)) return; + if (!navigateHistory) return; const selected = this.selectHistory(up); if (selected) { @@ -959,10 +962,8 @@ export default class MessageComposerInput extends React.Component { this.moveAutocompleteSelection(up); e.preventDefault(); } -*/ }; -/* selectHistory = async (up) => { const delta = up ? -1 : 1; @@ -984,26 +985,19 @@ export default class MessageComposerInput extends React.Component { return; } - const newContent = this.historyManager.getItem(delta, this.state.isRichtextEnabled ? 'rich' : 'markdown'); - if (!newContent) return false; - let editorState = EditorState.push( - this.state.editorState, - newContent, - 'insert-characters', - ); + let editorState = this.historyManager.getItem(delta, this.state.isRichtextEnabled ? 'rich' : 'markdown'); // Move selection to the end of the selected history - let newSelection = SelectionState.createEmpty(newContent.getLastBlock().getKey()); - newSelection = newSelection.merge({ - focusOffset: newContent.getLastBlock().getLength(), - anchorOffset: newContent.getLastBlock().getLength(), - }); - editorState = EditorState.forceSelection(editorState, newSelection); + const change = editorState.change().collapseToEndOf(editorState.document); + // XXX: should we be calling this.onChange(change) now? + // we skip it for now given we know we're about to setState anyway + editorState = change.value; - this.setState({editorState}); + this.setState({ editorState }, ()=>{ + this.refs.editor.focus(); + }); return true; }; -*/ onTab = async (e) => { this.setState({ @@ -1236,7 +1230,7 @@ export default class MessageComposerInput extends React.Component { className="mx_MessageComposer_editor" placeholder={this.props.placeholder} value={this.state.editorState} - onChange={this.onEditorContentChanged} + onChange={this.onChange} onKeyDown={this.onKeyDown} /* blockStyleFn={MessageComposerInput.getBlockStyle} From 984961a3ed22ddd233266c9f014c0a71fc1d05bd Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Tue, 8 May 2018 09:49:53 +0100 Subject: [PATCH 009/123] blind fix to the overlapping sticker bug --- src/components/views/messages/MStickerBody.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/views/messages/MStickerBody.js b/src/components/views/messages/MStickerBody.js index 08ddb6de20..501db5e22b 100644 --- a/src/components/views/messages/MStickerBody.js +++ b/src/components/views/messages/MStickerBody.js @@ -40,6 +40,7 @@ export default class MStickerBody extends MImageBody { } _onImageLoad() { + this.fixupHeight(); this.setState({ placeholderClasses: 'mx_MStickerBody_placeholder_invisible', }); From cbb8432873937e0fb4c03b26a5f7194b7cac907a Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 9 May 2018 01:03:40 +0100 Subject: [PATCH 010/123] unbreak switching from draft to slate --- src/ComposerHistoryManager.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index 9a6970a376..ce0eb8f0c3 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -74,7 +74,7 @@ class HistoryItem { return this.value; } } - log.error("unknown format -> outputFormat conversion"); + console.error("unknown format -> outputFormat conversion"); return this.value; } } @@ -91,9 +91,14 @@ export default class ComposerHistoryManager { // TODO: Performance issues? let item; for (; item = sessionStorage.getItem(`${this.prefix}[${this.currentIndex}]`); this.currentIndex++) { - this.history.push( - HistoryItem.fromJSON(JSON.parse(item)) - ); + try { + this.history.push( + HistoryItem.fromJSON(JSON.parse(item)) + ); + } + catch (e) { + console.warn("Throwing away unserialisable history", e); + } } this.lastIndex = this.currentIndex; } From 410a1683fe05862e1b777aa32ba9cfe7fb0f069f Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 12 May 2018 01:10:38 +0100 Subject: [PATCH 011/123] make autocomplete selection work --- .../views/rooms/MessageComposerInput.js | 203 ++++++++++-------- 1 file changed, 112 insertions(+), 91 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 4b950c429d..f50acb8bef 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -20,7 +20,7 @@ import PropTypes from 'prop-types'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import { Editor } from 'slate-react'; -import { Value, Document, Event } from 'slate'; +import { Value, Document, Event, Inline, Range, Node } from 'slate'; import Html from 'slate-html-serializer'; import { Markdown as Md } from 'slate-md-serializer'; @@ -197,49 +197,6 @@ export default class MessageComposerInput extends React.Component { * - contentState was passed in */ createEditorState(richText: boolean, value: ?Value): Value { -/* - const decorators = richText ? RichText.getScopedRTDecorators(this.props) : - RichText.getScopedMDDecorators(this.props); - const shouldShowPillAvatar = !SettingsStore.getValue("Pill.shouldHidePillAvatar"); - decorators.push({ - strategy: this.findPillEntities.bind(this), - component: (entityProps) => { - const Pill = sdk.getComponent('elements.Pill'); - const type = entityProps.contentState.getEntity(entityProps.entityKey).getType(); - const {url} = entityProps.contentState.getEntity(entityProps.entityKey).getData(); - if (type === ENTITY_TYPES.AT_ROOM_PILL) { - return ; - } else if (Pill.isPillUrl(url)) { - return ; - } - - return ( - - { entityProps.children } - - ); - }, - }); - const compositeDecorator = new CompositeDecorator(decorators); - let editorState = null; - if (contentState) { - editorState = EditorState.createWithContent(contentState, compositeDecorator); - } else { - editorState = EditorState.createEmpty(compositeDecorator); - } - - return EditorState.moveFocusToEnd(editorState); -*/ if (value instanceof Value) { return value; } @@ -566,6 +523,10 @@ export default class MessageComposerInput extends React.Component { return this.onVerticalArrow(ev, true); case KeyCode.DOWN: return this.onVerticalArrow(ev, false); + case KeyCode.TAB: + return this.onTab(ev); + case KeyCode.ESCAPE: + return this.onEscape(ev); default: // don't intercept it return; @@ -938,15 +899,19 @@ export default class MessageComposerInput extends React.Component { let navigateHistory = false; if (up) { - let scrollCorrection = editorNode.scrollTop; - if (cursorRect.top - editorRect.top + scrollCorrection == 0) { + const scrollCorrection = editorNode.scrollTop; + const distanceFromTop = cursorRect.top - editorRect.top + scrollCorrection; + //console.log(`Cursor distance from editor top is ${distanceFromTop}`); + if (distanceFromTop == 0) { navigateHistory = true; } } else { - let scrollCorrection = + const scrollCorrection = editorNode.scrollHeight - editorNode.clientHeight - editorNode.scrollTop; - if (cursorRect.bottom - editorRect.bottom + scrollCorrection == 0) { + const distanceFromBottom = cursorRect.bottom - editorRect.bottom + scrollCorrection; + //console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); + if (distanceFromBottom == 0) { navigateHistory = true; } } @@ -1033,38 +998,50 @@ export default class MessageComposerInput extends React.Component { * If passed a non-null displayedCompletion, modifies state.originalEditorState to compute new state.editorState. */ setDisplayedCompletion = async (displayedCompletion: ?Completion): boolean => { -/* const activeEditorState = this.state.originalEditorState || this.state.editorState; if (displayedCompletion == null) { if (this.state.originalEditorState) { let editorState = this.state.originalEditorState; - // This is a workaround from https://github.com/facebook/draft-js/issues/458 - // Due to the way we swap editorStates, Draft does not rerender at times - editorState = EditorState.forceSelection(editorState, - editorState.getSelection()); this.setState({editorState}); } return false; } const {range = null, completion = '', href = null, suffix = ''} = displayedCompletion; - let contentState = activeEditorState.getCurrentContent(); - let entityKey; + let inline; if (href) { - contentState = contentState.createEntity('LINK', 'IMMUTABLE', { - url: href, - isCompletion: true, + inline = Inline.create({ + type: 'pill', + isVoid: true, + data: { url: href }, }); - entityKey = contentState.getLastCreatedEntityKey(); } else if (completion === '@room') { - contentState = contentState.createEntity(ENTITY_TYPES.AT_ROOM_PILL, 'IMMUTABLE', { - isCompletion: true, + inline = Inline.create({ + type: 'pill', + isVoid: true, + data: { type: Pill.TYPE_AT_ROOM_MENTION }, }); - entityKey = contentState.getLastCreatedEntityKey(); } + let editorState = activeEditorState; + + if (range) { + const change = editorState.change().moveOffsetsTo(range.start, range.end); + editorState = change.value; + } + + const change = editorState.change().insertInlineAtRange( + editorState.selection, inline + ); + editorState = change.value; + + this.setState({ editorState, originalEditorState: activeEditorState }, ()=>{ +// this.refs.editor.focus(); + }); + +/* let selection; if (range) { selection = RichText.textOffsetsToSelectionState( @@ -1085,16 +1062,51 @@ export default class MessageComposerInput extends React.Component { let editorState = EditorState.push(activeEditorState, contentState, 'insert-characters'); editorState = EditorState.forceSelection(editorState, contentState.getSelectionAfter()); this.setState({editorState, originalEditorState: activeEditorState}); - - // for some reason, doing this right away does not update the editor :( - // setTimeout(() => this.refs.editor.focus(), 50); -*/ +*/ return true; }; + renderNode = props => { + const { attributes, children, node, isSelected } = props; + + switch (node.type) { + case 'paragraph': { + return

{children}

+ } + case 'pill': { + const { data, text } = node; + const url = data.get('url'); + const type = data.get('type'); + + const shouldShowPillAvatar = !SettingsStore.getValue("Pill.shouldHidePillAvatar"); + const Pill = sdk.getComponent('elements.Pill'); + + if (type === Pill.TYPE_AT_ROOM_MENTION) { + return ; + } + else if (Pill.isPillUrl(url)) { + return ; + } + else { + return + { text } + ; + } + } + } + }; + onFormatButtonClicked = (name: "bold" | "italic" | "strike" | "code" | "underline" | "quote" | "bullet" | "numbullet", e) => { e.preventDefault(); // don't steal focus from the editor! -/* + const command = { code: 'code-block', quote: 'blockquote', @@ -1102,7 +1114,6 @@ export default class MessageComposerInput extends React.Component { numbullet: 'ordered-list-item', }[name] || name; this.handleKeyCommand(command); -*/ }; /* returns inline style and block type of current SelectionState so MessageComposer can render formatting @@ -1140,14 +1151,41 @@ export default class MessageComposerInput extends React.Component { */ } - getAutocompleteQuery(contentState: ContentState) { - return ''; + getAutocompleteQuery(editorState: Value) { + // FIXME: do we really want to regenerate this every time the control is rerendered? + + // We can just return the current block where the selection begins, which + // should be enough to capture any autocompletion input, given autocompletion + // providers only search for the first match which intersects with the current selection. + // This avoids us having to serialize the whole thing to plaintext and convert + // selection offsets in & out of the plaintext domain. + return editorState.document.getDescendant(editorState.selection.anchorKey).text; // Don't send markdown links to the autocompleter // return this.removeMDLinks(contentState, ['@', '#']); } + getSelectionRange(editorState: Value) { + // return a character range suitable for handing to an autocomplete provider. + // the range is relative to the anchor of the current editor selection. + // if the selection spans multiple blocks, then we collapse it for the calculation. + const range = { + start: editorState.selection.anchorOffset, + end: (editorState.selection.anchorKey == editorState.selection.focusKey) ? + editorState.selection.focusOffset : editorState.selection.anchorOffset, + } + if (range.start > range.end) { + const tmp = range.start; + range.start = range.end; + range.end = tmp; + } + return range; + } + /* + // delinkifies any matrix.to markdown links (i.e. pills) of form + // [#foo:matrix.org](https://matrix.to/#/#foo:matrix.org). + // the prefixes is an array of sigils that will be matched on. removeMDLinks(contentState: ContentState, prefixes: string[]) { const plaintext = contentState.getPlainText(); if (!plaintext) return ''; @@ -1189,24 +1227,10 @@ export default class MessageComposerInput extends React.Component { render() { const activeEditorState = this.state.originalEditorState || this.state.editorState; - let hidePlaceholder = false; - // FIXME: in case we need to implement manual placeholdering - const className = classNames('mx_MessageComposer_input', { - mx_MessageComposer_input_empty: hidePlaceholder, mx_MessageComposer_input_error: this.state.someCompletions === false, }); - const content = null; - const selection = { - start: 0, - end: 0, - }; - - // const content = activeEditorState.getCurrentContent(); - // const selection = RichText.selectionStateToTextOffsets(activeEditorState.getSelection(), - // activeEditorState.getCurrentContent().getBlocksAsArray()); - return (
@@ -1216,8 +1240,8 @@ export default class MessageComposerInput extends React.Component { room={this.props.room} onConfirm={this.setDisplayedCompletion} onSelectionChange={this.setDisplayedCompletion} - query={this.getAutocompleteQuery(content)} - selection={selection} + query={this.getAutocompleteQuery(activeEditorState)} + selection={this.getSelectionRange(activeEditorState)} />
@@ -1232,6 +1256,8 @@ export default class MessageComposerInput extends React.Component { value={this.state.editorState} onChange={this.onChange} onKeyDown={this.onKeyDown} + renderNode={this.renderNode} + spellCheck={true} /* blockStyleFn={MessageComposerInput.getBlockStyle} keyBindingFn={MessageComposerInput.getKeyBinding} @@ -1240,11 +1266,6 @@ export default class MessageComposerInput extends React.Component { handlePastedText={this.onTextPasted} handlePastedFiles={this.props.onFilesPasted} stripPastedStyles={!this.state.isRichtextEnabled} - onTab={this.onTab} - onUpArrow={this.onUpArrow} - onDownArrow={this.onDownArrow} - onEscape={this.onEscape} - spellCheck={true} */ />
From d7c2c8ba7bfc8229176a588cdf530295e438792e Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 12 May 2018 16:21:36 +0100 Subject: [PATCH 012/123] include the plaintext representation of a pill within it --- src/components/views/rooms/MessageComposerInput.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index f50acb8bef..e1c5d1a190 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -20,7 +20,7 @@ import PropTypes from 'prop-types'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import { Editor } from 'slate-react'; -import { Value, Document, Event, Inline, Range, Node } from 'slate'; +import { Value, Document, Event, Inline, Text, Range, Node } from 'slate'; import Html from 'slate-html-serializer'; import { Markdown as Md } from 'slate-md-serializer'; @@ -781,6 +781,7 @@ export default class MessageComposerInput extends React.Component { ); } } else { + // Use the original contentState because `contentText` has had mentions // stripped and these need to end up in contentHTML. @@ -1014,14 +1015,14 @@ export default class MessageComposerInput extends React.Component { if (href) { inline = Inline.create({ type: 'pill', - isVoid: true, data: { url: href }, + nodes: [Text.create(completion)], }); } else if (completion === '@room') { inline = Inline.create({ type: 'pill', - isVoid: true, data: { type: Pill.TYPE_AT_ROOM_MENTION }, + nodes: [Text.create(completion)], }); } @@ -1262,7 +1263,6 @@ export default class MessageComposerInput extends React.Component { blockStyleFn={MessageComposerInput.getBlockStyle} keyBindingFn={MessageComposerInput.getKeyBinding} handleKeyCommand={this.handleKeyCommand} - handleReturn={this.handleReturn} handlePastedText={this.onTextPasted} handlePastedFiles={this.props.onFilesPasted} stripPastedStyles={!this.state.isRichtextEnabled} From 9c0c806af4b272458cdd8874347982d86cc70ea0 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 12 May 2018 20:04:58 +0100 Subject: [PATCH 013/123] correctly send pills in messages --- src/Markdown.js | 13 ++- src/autocomplete/NotifProvider.js | 1 + src/autocomplete/PlainWithPillsSerializer.js | 89 ++++++++++++++ src/autocomplete/RoomProvider.js | 1 + src/autocomplete/UserProvider.js | 1 + src/components/views/rooms/Autocomplete.js | 2 - .../views/rooms/MessageComposerInput.js | 109 +++++++++--------- src/stores/MessageComposerStore.js | 6 +- 8 files changed, 159 insertions(+), 63 deletions(-) create mode 100644 src/autocomplete/PlainWithPillsSerializer.js diff --git a/src/Markdown.js b/src/Markdown.js index aa1c7e45b1..e67f4df4fd 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -133,7 +133,10 @@ export default class Markdown { * Render the markdown message to plain text. That is, essentially * just remove any backslashes escaping what would otherwise be * markdown syntax - * (to fix https://github.com/vector-im/riot-web/issues/2870) + * (to fix https://github.com/vector-im/riot-web/issues/2870). + * + * N.B. this does **NOT** render arbitrary MD to plain text - only MD + * which has no formatting. Otherwise it emits HTML(!). */ toPlaintext() { const renderer = new commonmark.HtmlRenderer({safe: false}); @@ -161,6 +164,14 @@ export default class Markdown { if (is_multi_line(node) && node.next) this.lit('\n\n'); }; + // convert MD links into console-friendly ' < http://foo >' style links + // ...except given this function never gets called with links, it's useless. + // renderer.link = function(node, entering) { + // if (!entering) { + // this.lit(` < ${node.destination} >`); + // } + // }; + return renderer.render(this.parsed); } } diff --git a/src/autocomplete/NotifProvider.js b/src/autocomplete/NotifProvider.js index b7ac645525..5d2f05ecb9 100644 --- a/src/autocomplete/NotifProvider.js +++ b/src/autocomplete/NotifProvider.js @@ -40,6 +40,7 @@ export default class NotifProvider extends AutocompleteProvider { if (command && command[0] && '@room'.startsWith(command[0]) && command[0].length > 1) { return [{ completion: '@room', + completionId: '@room', suffix: ' ', component: ( } title="@room" description={_t("Notify the whole room")} /> diff --git a/src/autocomplete/PlainWithPillsSerializer.js b/src/autocomplete/PlainWithPillsSerializer.js new file mode 100644 index 0000000000..77391d5bbb --- /dev/null +++ b/src/autocomplete/PlainWithPillsSerializer.js @@ -0,0 +1,89 @@ +/* +Copyright 2018 New Vector Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Based originally on slate-plain-serializer + +import { Block } from 'slate'; + +/** + * Plain text serializer, which converts a Slate `value` to a plain text string, + * serializing pills into various different formats as required. + * + * @type {PlainWithPillsSerializer} + */ + +class PlainWithPillsSerializer { + + /* + * @param {String} options.pillFormat - either 'md', 'plain', 'id' + */ + constructor(options = {}) { + let { + pillFormat = 'plain', + } = options; + this.pillFormat = pillFormat; + } + + /** + * Serialize a Slate `value` to a plain text string, + * serializing pills as either MD links, plain text representations or + * ID representations as required. + * + * @param {Value} value + * @return {String} + */ + serialize = value => { + return this._serializeNode(value.document) + } + + /** + * Serialize a `node` to plain text. + * + * @param {Node} node + * @return {String} + */ + _serializeNode = node => { + if ( + node.object == 'document' || + (node.object == 'block' && Block.isBlockList(node.nodes)) + ) { + return node.nodes.map(this._serializeNode).join('\n'); + } else if (node.type == 'pill') { + switch (this.pillFormat) { + case 'plain': + return node.text; + case 'md': + return `[${ node.text }](${ node.data.get('url') })`; + case 'id': + return node.data.completionId || node.text; + } + } + else if (node.nodes) { + return node.nodes.map(this._serializeNode).join(''); + } + else { + return node.text; + } + } +} + +/** + * Export. + * + * @type {PlainWithPillsSerializer} + */ + +export default PlainWithPillsSerializer \ No newline at end of file diff --git a/src/autocomplete/RoomProvider.js b/src/autocomplete/RoomProvider.js index 31599703c2..b9346e75cc 100644 --- a/src/autocomplete/RoomProvider.js +++ b/src/autocomplete/RoomProvider.js @@ -78,6 +78,7 @@ export default class RoomProvider extends AutocompleteProvider { const displayAlias = getDisplayAliasForRoom(room.room) || room.roomId; return { completion: displayAlias, + completionId: displayAlias, suffix: ' ', href: makeRoomPermalink(displayAlias), component: ( diff --git a/src/autocomplete/UserProvider.js b/src/autocomplete/UserProvider.js index ce8f1020a1..a6dd13051b 100644 --- a/src/autocomplete/UserProvider.js +++ b/src/autocomplete/UserProvider.js @@ -113,6 +113,7 @@ export default class UserProvider extends AutocompleteProvider { // Length of completion should equal length of text in decorator. draft-js // relies on the length of the entity === length of the text in the decoration. completion: user.rawDisplayName.replace(' (IRC)', ''), + completionId: user.userId, suffix: range.start === 0 ? ': ' : ' ', href: makeUserPermalink(user.userId), component: ( diff --git a/src/components/views/rooms/Autocomplete.js b/src/components/views/rooms/Autocomplete.js index 4fb2a29381..5b56727705 100644 --- a/src/components/views/rooms/Autocomplete.js +++ b/src/components/views/rooms/Autocomplete.js @@ -263,7 +263,6 @@ export default class Autocomplete extends React.Component { const componentPosition = position; position++; - const onMouseMove = () => this.setSelection(componentPosition); const onClick = () => { this.setSelection(componentPosition); this.onCompletionClicked(); @@ -273,7 +272,6 @@ export default class Autocomplete extends React.Component { key: i, ref: `completion${position - 1}`, className, - onMouseMove, onClick, }); }); diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index e1c5d1a190..1bdbb86549 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -25,6 +25,7 @@ import { Value, Document, Event, Inline, Text, Range, Node } from 'slate'; import Html from 'slate-html-serializer'; import { Markdown as Md } from 'slate-md-serializer'; import Plain from 'slate-plain-serializer'; +import PlainWithPillsSerializer from "../../../autocomplete/PlainWithPillsSerializer"; // import {Editor, EditorState, RichUtils, CompositeDecorator, Modifier, // getDefaultKeyBinding, KeyBindingUtil, ContentState, ContentBlock, SelectionState, @@ -157,7 +158,7 @@ export default class MessageComposerInput extends React.Component { // the currently displayed editor state (note: this is always what is modified on input) editorState: this.createEditorState( isRichtextEnabled, - MessageComposerStore.getContentState(this.props.room.roomId), + MessageComposerStore.getEditorState(this.props.room.roomId), ), // the original editor state, before we started tabbing through completions @@ -172,6 +173,10 @@ export default class MessageComposerInput extends React.Component { }; this.client = MatrixClientPeg.get(); + + this.plainWithMdPills = new PlainWithPillsSerializer({ pillFormat: 'md' }); + this.plainWithIdPills = new PlainWithPillsSerializer({ pillFormat: 'id' }); + this.plainWithPlainPills = new PlainWithPillsSerializer({ pillFormat: 'plain' }); } /* @@ -686,30 +691,27 @@ export default class MessageComposerInput extends React.Component { return false; } */ - const contentState = this.state.editorState; + const editorState = this.state.editorState; - let contentText = Plain.serialize(contentState); + let contentText; let contentHTML; - if (contentText === '') return true; + // only look for commands if the first block contains simple unformatted text + // i.e. no pills or rich-text formatting. + let cmd, commandText; + const firstChild = editorState.document.nodes.get(0); + const firstGrandChild = firstChild && firstChild.nodes.get(0); + if (firstChild && firstGrandChild && + firstChild.object === 'block' && firstGrandChild.object === 'text' && + firstGrandChild.text[0] === '/' && firstGrandChild.text[1] !== '/') + { + commandText = this.plainWithIdPills.serialize(editorState); + cmd = SlashCommands.processInput(this.props.room.roomId, commandText); + } -/* - // Strip MD user (tab-completed) mentions to preserve plaintext mention behaviour. - // We have to do this now as opposed to after calculating the contentText for MD - // mode because entity positions may not be maintained when using - // md.toPlaintext(). - // Unfortunately this means we lose mentions in history when in MD mode. This - // would be fixed if history was stored as contentState. - contentText = this.removeMDLinks(contentState, ['@']); - - // Some commands (/join) require pills to be replaced with their text content - const commandText = this.removeMDLinks(contentState, ['#']); -*/ - const commandText = contentText; - const cmd = SlashCommands.processInput(this.props.room.roomId, commandText); if (cmd) { if (!cmd.error) { - this.historyManager.save(contentState, this.state.isRichtextEnabled ? 'rich' : 'markdown'); + this.historyManager.save(editorState, this.state.isRichtextEnabled ? 'rich' : 'markdown'); this.setState({ editorState: this.createEditorState(), }); @@ -774,46 +776,31 @@ export default class MessageComposerInput extends React.Component { shouldSendHTML = hasLink; } */ + contentText = this.plainWithPlainPills.serialize(editorState); + if (contentText === '') return true; + let shouldSendHTML = true; if (shouldSendHTML) { contentHTML = HtmlUtils.processHtmlForSending( - RichText.editorStateToHTML(contentState), + RichText.editorStateToHTML(editorState), ); } } else { + const sourceWithPills = this.plainWithMdPills.serialize(editorState); + if (sourceWithPills === '') return true; - // Use the original contentState because `contentText` has had mentions - // stripped and these need to end up in contentHTML. + const mdWithPills = new Markdown(sourceWithPills); -/* - // Replace all Entities of type `LINK` with markdown link equivalents. - // TODO: move this into `Markdown` and do the same conversion in the other - // two places (toggling from MD->RT mode and loading MD history into RT mode) - // but this can only be done when history includes Entities. - const pt = contentState.getBlocksAsArray().map((block) => { - let blockText = block.getText(); - let offset = 0; - this.findPillEntities(contentState, block, (start, end) => { - const entity = contentState.getEntity(block.getEntityAt(start)); - if (entity.getType() !== 'LINK') { - return; - } - const text = blockText.slice(offset + start, offset + end); - const url = entity.getData().url; - const mdLink = `[${text}](${url})`; - blockText = blockText.slice(0, offset + start) + mdLink + blockText.slice(offset + end); - offset += mdLink.length - text.length; - }); - return blockText; - }).join('\n'); -*/ - const md = new Markdown(contentText); // if contains no HTML and we're not quoting (needing HTML) - if (md.isPlainText() && !mustSendHTML) { - contentText = md.toPlaintext(); + if (mdWithPills.isPlainText() && !mustSendHTML) { + // N.B. toPlainText is only usable here because we know that the MD + // didn't contain any formatting in the first place... + contentText = mdWithPills.toPlaintext(); } else { - contentText = md.toPlaintext(); - contentHTML = md.toHTML(); + // to avoid ugliness clients which can't parse HTML we don't send pills + // in the plaintext body. + contentText = this.plainWithPlainPills.serialize(editorState); + contentHTML = mdWithPills.toHTML(); } } @@ -821,11 +808,11 @@ export default class MessageComposerInput extends React.Component { let sendTextFn = ContentHelpers.makeTextMessage; this.historyManager.save( - contentState, + editorState, this.state.isRichtextEnabled ? 'rich' : 'markdown', ); - if (contentText.startsWith('/me')) { + if (commandText && commandText.startsWith('/me')) { if (replyingToEv) { const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); Modal.createTrackedDialog('Emote Reply Fail', '', ErrorDialog, { @@ -842,14 +829,16 @@ export default class MessageComposerInput extends React.Component { sendTextFn = ContentHelpers.makeEmoteMessage; } - - let content = contentHTML ? sendHtmlFn(contentText, contentHTML) : sendTextFn(contentText); + let content = contentHTML ? + sendHtmlFn(contentText, contentHTML) : + sendTextFn(contentText); if (replyingToEv) { const replyContent = ReplyThread.makeReplyMixIn(replyingToEv); content = Object.assign(replyContent, content); - // Part of Replies fallback support - prepend the text we're sending with the text we're replying to + // Part of Replies fallback support - prepend the text we're sending + // with the text we're replying to const nestedReply = ReplyThread.getNestedReplyText(replyingToEv); if (nestedReply) { if (content.formatted_body) { @@ -1009,20 +998,26 @@ export default class MessageComposerInput extends React.Component { return false; } - const {range = null, completion = '', href = null, suffix = ''} = displayedCompletion; + const { + range = null, + completion = '', + completionId = '', + href = null, + suffix = '' + } = displayedCompletion; let inline; if (href) { inline = Inline.create({ type: 'pill', data: { url: href }, - nodes: [Text.create(completion)], + nodes: [Text.create(completionId || completion)], }); } else if (completion === '@room') { inline = Inline.create({ type: 'pill', data: { type: Pill.TYPE_AT_ROOM_MENTION }, - nodes: [Text.create(completion)], + nodes: [Text.create(completionId || completion)], }); } diff --git a/src/stores/MessageComposerStore.js b/src/stores/MessageComposerStore.js index 3b1ab1fa72..0e6c856e1b 100644 --- a/src/stores/MessageComposerStore.js +++ b/src/stores/MessageComposerStore.js @@ -44,7 +44,7 @@ class MessageComposerStore extends Store { __onDispatch(payload) { switch (payload.action) { case 'editor_state': - this._contentState(payload); + this._editorState(payload); break; case 'on_logged_out': this.reset(); @@ -52,7 +52,7 @@ class MessageComposerStore extends Store { } } - _contentState(payload) { + _editorState(payload) { const editorStateMap = this._state.editorStateMap; editorStateMap[payload.room_id] = payload.editor_state; localStorage.setItem('editor_state', JSON.stringify(editorStateMap)); @@ -61,7 +61,7 @@ class MessageComposerStore extends Store { }); } - getContentState(roomId) { + getEditorState(roomId) { return this._state.editorStateMap[roomId]; } From c91dcffe82605c7ca1cb3693029089a6438cab06 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 00:40:54 +0100 Subject: [PATCH 014/123] fix cursor behaviour around pills --- src/autocomplete/PlainWithPillsSerializer.js | 4 +- .../views/rooms/MessageComposerInput.js | 48 ++++++++++++++++--- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/autocomplete/PlainWithPillsSerializer.js b/src/autocomplete/PlainWithPillsSerializer.js index 77391d5bbb..6827f1fe73 100644 --- a/src/autocomplete/PlainWithPillsSerializer.js +++ b/src/autocomplete/PlainWithPillsSerializer.js @@ -64,11 +64,11 @@ class PlainWithPillsSerializer { } else if (node.type == 'pill') { switch (this.pillFormat) { case 'plain': - return node.text; + return node.data.get('completion'); case 'md': return `[${ node.text }](${ node.data.get('url') })`; case 'id': - return node.data.completionId || node.text; + return node.data.get('completionId') || node.data.get('completion'); } } else if (node.nodes) { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 1bdbb86549..dfc0a2ee21 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -177,6 +177,8 @@ export default class MessageComposerInput extends React.Component { this.plainWithMdPills = new PlainWithPillsSerializer({ pillFormat: 'md' }); this.plainWithIdPills = new PlainWithPillsSerializer({ pillFormat: 'id' }); this.plainWithPlainPills = new PlainWithPillsSerializer({ pillFormat: 'plain' }); + + this.direction = ''; } /* @@ -358,6 +360,17 @@ export default class MessageComposerInput extends React.Component { } onChange = (change: Change) => { + + let editorState = change.value; + + if (this.direction !== '') { + const focusedNode = editorState.focusInline || editorState.focusText; + if (focusedNode.isVoid) { + change = change[`collapseToEndOf${ this.direction }Text`](); + editorState = change.value; + } + } + /* editorState = RichText.attachImmutableEntitiesToEmoji(editorState); @@ -417,7 +430,7 @@ export default class MessageComposerInput extends React.Component { */ /* Since a modification was made, set originalEditorState to null, since newState is now our original */ this.setState({ - editorState: change.value, + editorState, originalEditorState: null, }); }; @@ -521,6 +534,18 @@ export default class MessageComposerInput extends React.Component { } onKeyDown = (ev: Event, change: Change, editor: Editor) => { + + // skip void nodes - see + // https://github.com/ianstormtaylor/slate/issues/762#issuecomment-304855095 + if (ev.keyCode === KeyCode.LEFT) { + this.direction = 'Previous'; + } + else if (ev.keyCode === KeyCode.RIGHT) { + this.direction = 'Next'; + } else { + this.direction = ''; + } + switch (ev.keyCode) { case KeyCode.ENTER: return this.handleReturn(ev); @@ -1010,14 +1035,23 @@ export default class MessageComposerInput extends React.Component { if (href) { inline = Inline.create({ type: 'pill', - data: { url: href }, - nodes: [Text.create(completionId || completion)], + data: { completion, completionId, url: href }, + // we can't put text in here otherwise the editor tries to select it + isVoid: true, + nodes: [], }); } else if (completion === '@room') { inline = Inline.create({ type: 'pill', - data: { type: Pill.TYPE_AT_ROOM_MENTION }, - nodes: [Text.create(completionId || completion)], + data: { completion, completionId }, + // we can't put text in here otherwise the editor tries to select it + isVoid: true, + nodes: [], + }); + } else { + inline = Inline.create({ + type: 'autocompletion', + nodes: [Text.create(completion)] }); } @@ -1072,12 +1106,12 @@ export default class MessageComposerInput extends React.Component { case 'pill': { const { data, text } = node; const url = data.get('url'); - const type = data.get('type'); + const completion = data.get('completion'); const shouldShowPillAvatar = !SettingsStore.getValue("Pill.shouldHidePillAvatar"); const Pill = sdk.getComponent('elements.Pill'); - if (type === Pill.TYPE_AT_ROOM_MENTION) { + if (completion === '@room') { return Date: Sun, 13 May 2018 00:48:52 +0100 Subject: [PATCH 015/123] fix NPEs when deleting mentions --- src/components/views/rooms/MessageComposerInput.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index dfc0a2ee21..919b38e741 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1038,7 +1038,6 @@ export default class MessageComposerInput extends React.Component { data: { completion, completionId, url: href }, // we can't put text in here otherwise the editor tries to select it isVoid: true, - nodes: [], }); } else if (completion === '@room') { inline = Inline.create({ @@ -1046,7 +1045,6 @@ export default class MessageComposerInput extends React.Component { data: { completion, completionId }, // we can't put text in here otherwise the editor tries to select it isVoid: true, - nodes: [], }); } else { inline = Inline.create({ From 877a6195ae9c73f29908286347639e9112ef5b0f Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 00:54:01 +0100 Subject: [PATCH 016/123] unbreak history scrolling for pills & emoji --- src/components/views/rooms/MessageComposerInput.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 919b38e741..a8a9188971 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -912,12 +912,17 @@ export default class MessageComposerInput extends React.Component { const editorNode = ReactDOM.findDOMNode(this.refs.editor); const editorRect = editorNode.getBoundingClientRect(); + // heuristic to handle tall emoji, pills, etc pushing the cursor away from the top + // or bottom of the page. + // XXX: is this going to break on large inline images or top-to-bottom scripts? + const EDGE_THRESHOLD = 8; + let navigateHistory = false; if (up) { const scrollCorrection = editorNode.scrollTop; const distanceFromTop = cursorRect.top - editorRect.top + scrollCorrection; - //console.log(`Cursor distance from editor top is ${distanceFromTop}`); - if (distanceFromTop == 0) { + console.log(`Cursor distance from editor top is ${distanceFromTop}`); + if (distanceFromTop < EDGE_THRESHOLD) { navigateHistory = true; } } @@ -925,8 +930,8 @@ export default class MessageComposerInput extends React.Component { const scrollCorrection = editorNode.scrollHeight - editorNode.clientHeight - editorNode.scrollTop; const distanceFromBottom = cursorRect.bottom - editorRect.bottom + scrollCorrection; - //console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); - if (distanceFromBottom == 0) { + console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); + if (distanceFromBottom < EDGE_THRESHOLD) { navigateHistory = true; } } From c967ecc4e59cc475feaed9f36a6f447fa327b139 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 03:04:40 +0100 Subject: [PATCH 017/123] autocomplete polishing * suppress autocomplete when navigating through history * only search for slashcommands if in the first block of the editor * handle suffix returns from providers correctly * fix SelectionRange typing in the providers * fix bugs when pressing ctrl-a, typing and then tab to complete a replacement by collapsing selection to anchor when inserting a completion in the editor * fix https://github.com/vector-im/riot-web/issues/4762 --- src/autocomplete/AutocompleteProvider.js | 12 +++++++++--- src/autocomplete/Autocompleter.js | 9 +++++---- src/autocomplete/CommandProvider.js | 4 +++- src/autocomplete/DuckDuckGoProvider.js | 3 ++- src/autocomplete/NotifProvider.js | 3 ++- src/autocomplete/RoomProvider.js | 9 ++------- src/autocomplete/UserProvider.js | 19 ++++++++----------- 7 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/autocomplete/AutocompleteProvider.js b/src/autocomplete/AutocompleteProvider.js index c93ae4fb2a..2d3bad14c5 100644 --- a/src/autocomplete/AutocompleteProvider.js +++ b/src/autocomplete/AutocompleteProvider.js @@ -20,13 +20,19 @@ import React from 'react'; import type {Completion, SelectionRange} from './Autocompleter'; export default class AutocompleteProvider { - constructor(commandRegex?: RegExp) { + constructor(commandRegex?: RegExp, forcedCommandRegex?: RegExp) { if (commandRegex) { if (!commandRegex.global) { throw new Error('commandRegex must have global flag set'); } this.commandRegex = commandRegex; } + if (forcedCommandRegex) { + if (!forcedCommandRegex.global) { + throw new Error('forcedCommandRegex must have global flag set'); + } + this.forcedCommandRegex = forcedCommandRegex; + } } destroy() { @@ -36,11 +42,11 @@ export default class AutocompleteProvider { /** * Of the matched commands in the query, returns the first that contains or is contained by the selection, or null. */ - getCurrentCommand(query: string, selection: {start: number, end: number}, force: boolean = false): ?string { + getCurrentCommand(query: string, selection: SelectionRange, force: boolean = false): ?string { let commandRegex = this.commandRegex; if (force && this.shouldForceComplete()) { - commandRegex = /\S+/g; + commandRegex = this.forcedCommandRegex || /\S+/g; } if (commandRegex == null) { diff --git a/src/autocomplete/Autocompleter.js b/src/autocomplete/Autocompleter.js index 3d30363d9f..db3ba5a7e1 100644 --- a/src/autocomplete/Autocompleter.js +++ b/src/autocomplete/Autocompleter.js @@ -27,6 +27,7 @@ import NotifProvider from './NotifProvider'; import Promise from 'bluebird'; export type SelectionRange = { + beginning: boolean, start: number, end: number }; @@ -77,12 +78,12 @@ export default class Autocompleter { // Array of inspections of promises that might timeout. Instead of allowing a // single timeout to reject the Promise.all, reflect each one and once they've all // settled, filter for the fulfilled ones - this.providers.map((provider) => { - return provider + this.providers.map(provider => + provider .getCompletions(query, selection, force) .timeout(PROVIDER_COMPLETION_TIMEOUT) - .reflect(); - }), + .reflect() + ), ); return completionsList.filter( diff --git a/src/autocomplete/CommandProvider.js b/src/autocomplete/CommandProvider.js index e33fa7861f..0545095cf0 100644 --- a/src/autocomplete/CommandProvider.js +++ b/src/autocomplete/CommandProvider.js @@ -21,6 +21,7 @@ import { _t, _td } from '../languageHandler'; import AutocompleteProvider from './AutocompleteProvider'; import FuzzyMatcher from './FuzzyMatcher'; import {TextualCompletion} from './Components'; +import type {SelectionRange} from './Autocompleter'; // TODO merge this with the factory mechanics of SlashCommands? // Warning: Since the description string will be translated in _t(result.description), all these strings below must be in i18n/strings/en_EN.json file @@ -123,8 +124,9 @@ export default class CommandProvider extends AutocompleteProvider { }); } - async getCompletions(query: string, selection: {start: number, end: number}) { + async getCompletions(query: string, selection: SelectionRange) { let completions = []; + if (!selection.beginning) return completions; const {command, range} = this.getCurrentCommand(query, selection); if (command) { completions = this.matcher.match(command[0]).map((result) => { diff --git a/src/autocomplete/DuckDuckGoProvider.js b/src/autocomplete/DuckDuckGoProvider.js index 68d4915f56..236ab49b62 100644 --- a/src/autocomplete/DuckDuckGoProvider.js +++ b/src/autocomplete/DuckDuckGoProvider.js @@ -22,6 +22,7 @@ import AutocompleteProvider from './AutocompleteProvider'; import 'whatwg-fetch'; import {TextualCompletion} from './Components'; +import type {SelectionRange} from './Autocompleter'; const DDG_REGEX = /\/ddg\s+(.+)$/g; const REFERRER = 'vector'; @@ -36,7 +37,7 @@ export default class DuckDuckGoProvider extends AutocompleteProvider { + `&format=json&no_redirect=1&no_html=1&t=${encodeURIComponent(REFERRER)}`; } - async getCompletions(query: string, selection: {start: number, end: number}) { + async getCompletions(query: string, selection: SelectionRange) { const {command, range} = this.getCurrentCommand(query, selection); if (!query || !command) { return []; diff --git a/src/autocomplete/NotifProvider.js b/src/autocomplete/NotifProvider.js index 5d2f05ecb9..a426528567 100644 --- a/src/autocomplete/NotifProvider.js +++ b/src/autocomplete/NotifProvider.js @@ -20,6 +20,7 @@ import { _t } from '../languageHandler'; import MatrixClientPeg from '../MatrixClientPeg'; import {PillCompletion} from './Components'; import sdk from '../index'; +import type {SelectionRange} from './Autocompleter'; const AT_ROOM_REGEX = /@\S*/g; @@ -29,7 +30,7 @@ export default class NotifProvider extends AutocompleteProvider { this.room = room; } - async getCompletions(query: string, selection: {start: number, end: number}, force = false) { + async getCompletions(query: string, selection: SelectionRange, force = false) { const RoomAvatar = sdk.getComponent('views.avatars.RoomAvatar'); const client = MatrixClientPeg.get(); diff --git a/src/autocomplete/RoomProvider.js b/src/autocomplete/RoomProvider.js index b9346e75cc..139ac87041 100644 --- a/src/autocomplete/RoomProvider.js +++ b/src/autocomplete/RoomProvider.js @@ -26,6 +26,7 @@ import {getDisplayAliasForRoom} from '../Rooms'; import sdk from '../index'; import _sortBy from 'lodash/sortBy'; import {makeRoomPermalink} from "../matrix-to"; +import type {SelectionRange} from './Autocompleter'; const ROOM_REGEX = /(?=#)(\S*)/g; @@ -46,15 +47,9 @@ export default class RoomProvider extends AutocompleteProvider { }); } - async getCompletions(query: string, selection: {start: number, end: number}, force = false) { + async getCompletions(query: string, selection: SelectionRange, force = false) { const RoomAvatar = sdk.getComponent('views.avatars.RoomAvatar'); - // Disable autocompletions when composing commands because of various issues - // (see https://github.com/vector-im/riot-web/issues/4762) - if (/^(\/join|\/leave)/.test(query)) { - return []; - } - const client = MatrixClientPeg.get(); let completions = []; const {command, range} = this.getCurrentCommand(query, selection, force); diff --git a/src/autocomplete/UserProvider.js b/src/autocomplete/UserProvider.js index a6dd13051b..c25dad8877 100644 --- a/src/autocomplete/UserProvider.js +++ b/src/autocomplete/UserProvider.js @@ -28,18 +28,21 @@ import _sortBy from 'lodash/sortBy'; import MatrixClientPeg from '../MatrixClientPeg'; import type {Room, RoomMember} from 'matrix-js-sdk'; +import type {SelectionRange} from './Autocompleter'; import {makeUserPermalink} from "../matrix-to"; const USER_REGEX = /@\S*/g; +// used when you hit 'tab' - we allow some separator chars at the beginning +// to allow you to tab-complete /mat into /(matthew) +const FORCED_USER_REGEX = /[^/,:; \t\n]\S*/g; + export default class UserProvider extends AutocompleteProvider { users: Array = null; room: Room = null; constructor(room) { - super(USER_REGEX, { - keys: ['name'], - }); + super(USER_REGEX, FORCED_USER_REGEX); this.room = room; this.matcher = new FuzzyMatcher([], { keys: ['name', 'userId'], @@ -87,15 +90,9 @@ export default class UserProvider extends AutocompleteProvider { this.users = null; } - async getCompletions(query: string, selection: {start: number, end: number}, force = false) { + async getCompletions(query: string, selection: SelectionRange, force = false) { const MemberAvatar = sdk.getComponent('views.avatars.MemberAvatar'); - // Disable autocompletions when composing commands because of various issues - // (see https://github.com/vector-im/riot-web/issues/4762) - if (/^(\/ban|\/unban|\/op|\/deop|\/invite|\/kick|\/verify)/.test(query)) { - return []; - } - // lazy-load user list into matcher if (this.users === null) this._makeUsers(); @@ -114,7 +111,7 @@ export default class UserProvider extends AutocompleteProvider { // relies on the length of the entity === length of the text in the decoration. completion: user.rawDisplayName.replace(' (IRC)', ''), completionId: user.userId, - suffix: range.start === 0 ? ': ' : ' ', + suffix: (selection.beginning && range.start === 0) ? ': ' : ' ', href: makeUserPermalink(user.userId), component: ( Date: Sun, 13 May 2018 03:16:55 +0100 Subject: [PATCH 018/123] autocomplete polishing * suppress autocomplete when navigating through history * only search for slashcommands if in the first block of the editor * handle suffix returns from providers correctly * fix bugs when pressing ctrl-a, typing and then tab to complete a replacement by collapsing selection to anchor when inserting a completion in the editor --- src/components/views/rooms/Autocomplete.js | 2 +- .../views/rooms/MessageComposerInput.js | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/components/views/rooms/Autocomplete.js b/src/components/views/rooms/Autocomplete.js index 5b56727705..4bc91b5c2b 100644 --- a/src/components/views/rooms/Autocomplete.js +++ b/src/components/views/rooms/Autocomplete.js @@ -114,7 +114,7 @@ export default class Autocomplete extends React.Component { processQuery(query, selection) { return this.autocompleter.getCompletions( - query, selection, this.state.forceComplete, + query, selection, this.state.forceComplete ).then((completions) => { // Only ever process the completions for the most recent query being processed if (query !== this.queryRequested) { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index a8a9188971..83760d932d 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -178,6 +178,7 @@ export default class MessageComposerInput extends React.Component { this.plainWithIdPills = new PlainWithPillsSerializer({ pillFormat: 'id' }); this.plainWithPlainPills = new PlainWithPillsSerializer({ pillFormat: 'plain' }); + this.suppressAutoComplete = false; this.direction = ''; } @@ -535,6 +536,8 @@ export default class MessageComposerInput extends React.Component { onKeyDown = (ev: Event, change: Change, editor: Editor) => { + this.suppressAutoComplete = false; + // skip void nodes - see // https://github.com/ianstormtaylor/slate/issues/762#issuecomment-304855095 if (ev.keyCode === KeyCode.LEFT) { @@ -978,6 +981,8 @@ export default class MessageComposerInput extends React.Component { // we skip it for now given we know we're about to setState anyway editorState = change.value; + this.suppressAutoComplete = true; + this.setState({ editorState }, ()=>{ this.refs.editor.focus(); }); @@ -1061,13 +1066,15 @@ export default class MessageComposerInput extends React.Component { let editorState = activeEditorState; if (range) { - const change = editorState.change().moveOffsetsTo(range.start, range.end); + const change = editorState.change() + .collapseToAnchor() + .moveOffsetsTo(range.start, range.end); editorState = change.value; } - const change = editorState.change().insertInlineAtRange( - editorState.selection, inline - ); + const change = editorState.change() + .insertInlineAtRange(editorState.selection, inline) + .insertText(suffix); editorState = change.value; this.setState({ editorState, originalEditorState: activeEditorState }, ()=>{ @@ -1185,13 +1192,12 @@ export default class MessageComposerInput extends React.Component { } getAutocompleteQuery(editorState: Value) { - // FIXME: do we really want to regenerate this every time the control is rerendered? - // We can just return the current block where the selection begins, which // should be enough to capture any autocompletion input, given autocompletion // providers only search for the first match which intersects with the current selection. // This avoids us having to serialize the whole thing to plaintext and convert // selection offsets in & out of the plaintext domain. + return editorState.document.getDescendant(editorState.selection.anchorKey).text; // Don't send markdown links to the autocompleter @@ -1199,10 +1205,19 @@ export default class MessageComposerInput extends React.Component { } getSelectionRange(editorState: Value) { + let beginning = false; + const query = this.getAutocompleteQuery(editorState); + const firstChild = editorState.document.nodes.get(0); + const firstGrandChild = firstChild && firstChild.nodes.get(0); + beginning = (firstChild && firstGrandChild && + firstChild.object === 'block' && firstGrandChild.object === 'text' && + editorState.selection.anchorKey === firstGrandChild.key); + // return a character range suitable for handing to an autocomplete provider. // the range is relative to the anchor of the current editor selection. // if the selection spans multiple blocks, then we collapse it for the calculation. const range = { + beginning, // whether the selection is in the first block of the editor or not start: editorState.selection.anchorOffset, end: (editorState.selection.anchorKey == editorState.selection.focusKey) ? editorState.selection.focusOffset : editorState.selection.anchorOffset, @@ -1273,7 +1288,7 @@ export default class MessageComposerInput extends React.Component { room={this.props.room} onConfirm={this.setDisplayedCompletion} onSelectionChange={this.setDisplayedCompletion} - query={this.getAutocompleteQuery(activeEditorState)} + query={ this.suppressAutoComplete ? '' : this.getAutocompleteQuery(activeEditorState) } selection={this.getSelectionRange(activeEditorState)} />
From e06763cd59da8b07255f46ab7e1abc4604c637cc Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 03:18:41 +0100 Subject: [PATCH 019/123] show all slashcommands on / --- src/autocomplete/CommandProvider.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/autocomplete/CommandProvider.js b/src/autocomplete/CommandProvider.js index 0545095cf0..4f2aed3dc6 100644 --- a/src/autocomplete/CommandProvider.js +++ b/src/autocomplete/CommandProvider.js @@ -129,7 +129,14 @@ export default class CommandProvider extends AutocompleteProvider { if (!selection.beginning) return completions; const {command, range} = this.getCurrentCommand(query, selection); if (command) { - completions = this.matcher.match(command[0]).map((result) => { + let results; + if (command[0] == '/') { + results = COMMANDS; + } + else { + results = this.matcher.match(command[0]); + } + completions = results.map((result) => { return { completion: result.command + ' ', component: ( Date: Sun, 13 May 2018 03:26:22 +0100 Subject: [PATCH 020/123] don't lose focus after a / command --- src/components/views/rooms/MessageComposerInput.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 83760d932d..a1b569b5f4 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -745,9 +745,10 @@ export default class MessageComposerInput extends React.Component { }); } if (cmd.promise) { - cmd.promise.then(function() { + cmd.promise.then(()=>{ console.log("Command success."); - }, function(err) { + this.refs.editor.focus(); + }, (err)=>{ console.error("Command failure: %s", err); const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); Modal.createTrackedDialog('Server error', '', ErrorDialog, { From 79f7c5d6ab0e7b0bad860039403fef195381cdbe Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 03:29:56 +0100 Subject: [PATCH 021/123] remove // support, as it never worked if you want to escape a /, do it with \/ or just precede with a space --- src/SlashCommands.js | 2 +- src/components/views/rooms/MessageComposerInput.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index d45e45e84c..7f32c1e25a 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -434,7 +434,7 @@ module.exports = { // trim any trailing whitespace, as it can confuse the parser for // IRC-style commands input = input.replace(/\s+$/, ""); - if (input[0] === "/" && input[1] !== "/") { + if (input[0] === "/") { const bits = input.match(/^(\S+?)( +((.|\n)*))?$/); let cmd; let args; diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index a1b569b5f4..2a59ccbe7d 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -731,7 +731,7 @@ export default class MessageComposerInput extends React.Component { const firstGrandChild = firstChild && firstChild.nodes.get(0); if (firstChild && firstGrandChild && firstChild.object === 'block' && firstGrandChild.object === 'text' && - firstGrandChild.text[0] === '/' && firstGrandChild.text[1] !== '/') + firstGrandChild.text[0] === '/') { commandText = this.plainWithIdPills.serialize(editorState); cmd = SlashCommands.processInput(this.props.room.roomId, commandText); From dd0726f06802666f531db8a7c5784797b7148010 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 21:17:43 +0100 Subject: [PATCH 022/123] fix navigating history downwards on tall messages; remove obsolete code --- src/RichText.js | 36 ------------------- .../views/rooms/MessageComposerInput.js | 32 +---------------- 2 files changed, 1 insertion(+), 67 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index 7ffb4dd785..d867636dc9 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -223,42 +223,6 @@ export function selectionStateToTextOffsets(selectionState: SelectionState, }; } -export function textOffsetsToSelectionState({start, end}: SelectionRange, - contentBlocks: Array): SelectionState { - let selectionState = SelectionState.createEmpty(); - // Subtract block lengths from `start` and `end` until they are less than the current - // block length (accounting for the NL at the end of each block). Set them to -1 to - // indicate that the corresponding selection state has been determined. - for (const block of contentBlocks) { - const blockLength = block.getLength(); - // -1 indicating that the position start position has been found - if (start !== -1) { - if (start < blockLength + 1) { - selectionState = selectionState.merge({ - anchorKey: block.getKey(), - anchorOffset: start, - }); - start = -1; // selection state for the start calculated - } else { - start -= blockLength + 1; // +1 to account for newline between blocks - } - } - // -1 indicating that the position end position has been found - if (end !== -1) { - if (end < blockLength + 1) { - selectionState = selectionState.merge({ - focusKey: block.getKey(), - focusOffset: end, - }); - end = -1; // selection state for the end calculated - } else { - end -= blockLength + 1; // +1 to account for newline between blocks - } - } - } - return selectionState; -} - // modified version of https://github.com/draft-js-plugins/draft-js-plugins/blob/master/draft-js-emoji-plugin/src/modifiers/attachImmutableEntitiesToEmojis.js export function attachImmutableEntitiesToEmoji(editorState: EditorState): EditorState { const contentState = editorState.getCurrentContent(); diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 2a59ccbe7d..3e8d3cd868 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -494,14 +494,6 @@ export default class MessageComposerInput extends React.Component { if (this.props.onContentChanged) { this.props.onContentChanged(textContent, selection); } - - // Scroll to the bottom of the editor if the cursor is on the last line of the - // composer. For some reason the editor won't scroll automatically if we paste - // blocks of text in or insert newlines. - if (textContent.slice(selection.start).indexOf("\n") === -1) { - let editorRoot = this.refs.editor.refs.editor.parentNode.parentNode; - editorRoot.scrollTop = editorRoot.scrollHeight; - } */ }); } @@ -933,7 +925,7 @@ export default class MessageComposerInput extends React.Component { else { const scrollCorrection = editorNode.scrollHeight - editorNode.clientHeight - editorNode.scrollTop; - const distanceFromBottom = cursorRect.bottom - editorRect.bottom + scrollCorrection; + const distanceFromBottom = editorRect.bottom - cursorRect.bottom + scrollCorrection; console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); if (distanceFromBottom < EDGE_THRESHOLD) { navigateHistory = true; @@ -1082,28 +1074,6 @@ export default class MessageComposerInput extends React.Component { // this.refs.editor.focus(); }); -/* - let selection; - if (range) { - selection = RichText.textOffsetsToSelectionState( - range, contentState.getBlocksAsArray(), - ); - } else { - selection = activeEditorState.getSelection(); - } - - contentState = Modifier.replaceText(contentState, selection, completion, null, entityKey); - - // Move the selection to the end of the block - const afterSelection = contentState.getSelectionAfter(); - if (suffix) { - contentState = Modifier.replaceText(contentState, afterSelection, suffix); - } - - let editorState = EditorState.push(activeEditorState, contentState, 'insert-characters'); - editorState = EditorState.forceSelection(editorState, contentState.getSelectionAfter()); - this.setState({editorState, originalEditorState: activeEditorState}); -*/ return true; }; From ddfe0691c43c309e5f4265e6663667c15da10557 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 22:41:39 +0100 Subject: [PATCH 023/123] fix insert_mention --- .../views/rooms/MessageComposerInput.js | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 3e8d3cd868..8b72dc1a0b 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -243,23 +243,24 @@ export default class MessageComposerInput extends React.Component { case 'focus_composer': editor.focus(); break; -/* - case 'insert_mention': { - // Pretend that we've autocompleted this user because keeping two code - // paths for inserting a user pill is not fun - const selection = this.state.editorState.getSelection(); - const member = this.props.room.getMember(payload.user_id); - const completion = member ? - member.rawDisplayName.replace(' (IRC)', '') : payload.user_id; - this.setDisplayedCompletion({ - completion, - selection, - href: makeUserPermalink(payload.user_id), - suffix: selection.getStartOffset() === 0 ? ': ' : ' ', - }); - } + case 'insert_mention': + { + // Pretend that we've autocompleted this user because keeping two code + // paths for inserting a user pill is not fun + const selection = this.getSelectionRange(this.state.editorState); + const member = this.props.room.getMember(payload.user_id); + const completion = member ? + member.rawDisplayName.replace(' (IRC)', '') : payload.user_id; + this.setDisplayedCompletion({ + completion, + completionId: payload.user_id, + selection, + href: makeUserPermalink(payload.user_id), + suffix: (selection.beginning && selection.start === 0) ? ': ' : ' ', + }); + } break; - +/* case 'quote': { // old quoting, whilst rich quoting is in labs /// XXX: Not doing rich-text quoting from formatted-body because draft-js /// has regressed such that when links are quoted, errors are thrown. See From a247ea2f77d0ad10faf63c3c798abb59c8b237d1 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 22:43:20 +0100 Subject: [PATCH 024/123] delete duplicate propTypes(!!!) --- .../views/rooms/MessageComposerInput.js | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 8b72dc1a0b..756c9eb1a9 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -100,6 +100,8 @@ export default class MessageComposerInput extends React.Component { // called with current plaintext content (as a string) whenever it changes onContentChanged: PropTypes.func, + onFilesPasted: PropTypes.func, + onInputStateChanged: PropTypes.func, }; @@ -1292,19 +1294,3 @@ export default class MessageComposerInput extends React.Component { ); } } - -MessageComposerInput.propTypes = { - // a callback which is called when the height of the composer is - // changed due to a change in content. - onResize: PropTypes.func, - - // js-sdk Room object - room: PropTypes.object.isRequired, - - // called with current plaintext content (as a string) whenever it changes - onContentChanged: PropTypes.func, - - onFilesPasted: PropTypes.func, - - onInputStateChanged: PropTypes.func, -}; From 7405b49b4416ab8803c64283f26c983097e8fd5d Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 23:34:00 +0100 Subject: [PATCH 025/123] unify setState() and onChange() also make emoji autocomplete work again also remove the onInputContentChanged prop also slateify the onInputStateChanged prop --- src/components/views/rooms/MessageComposer.js | 18 +- .../views/rooms/MessageComposerInput.js | 163 +++++------------- 2 files changed, 42 insertions(+), 139 deletions(-) diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 28a90b375a..9aaa33f0fa 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -35,7 +35,6 @@ export default class MessageComposer extends React.Component { this.onUploadFileSelected = this.onUploadFileSelected.bind(this); this.uploadFiles = this.uploadFiles.bind(this); this.onVoiceCallClick = this.onVoiceCallClick.bind(this); - this.onInputContentChanged = this.onInputContentChanged.bind(this); this._onAutocompleteConfirm = this._onAutocompleteConfirm.bind(this); this.onToggleFormattingClicked = this.onToggleFormattingClicked.bind(this); this.onToggleMarkdownClicked = this.onToggleMarkdownClicked.bind(this); @@ -44,13 +43,10 @@ export default class MessageComposer extends React.Component { this._onRoomViewStoreUpdate = this._onRoomViewStoreUpdate.bind(this); this.state = { - autocompleteQuery: '', - selection: null, inputState: { - style: [], + marks: [], blockType: null, isRichtextEnabled: SettingsStore.getValue('MessageComposerInput.isRichTextEnabled'), - wordCount: 0, }, showFormatting: SettingsStore.getValue('MessageComposer.showFormatting'), isQuoting: Boolean(RoomViewStore.getQuotingEvent()), @@ -209,13 +205,6 @@ export default class MessageComposer extends React.Component { // this._startCallApp(true); } - onInputContentChanged(content: string, selection: {start: number, end: number}) { - this.setState({ - autocompleteQuery: content, - selection, - }); - } - onInputStateChanged(inputState) { this.setState({inputState}); } @@ -348,7 +337,6 @@ export default class MessageComposer extends React.Component { room={this.props.room} placeholder={placeholderText} onFilesPasted={this.uploadFiles} - onContentChanged={this.onInputContentChanged} onInputStateChanged={this.onInputStateChanged} />, formattingButton, stickerpickerButton, @@ -365,10 +353,10 @@ export default class MessageComposer extends React.Component { ); } - const {style, blockType} = this.state.inputState; + const {marks, blockType} = this.state.inputState; const formatButtons = ["bold", "italic", "strike", "underline", "code", "quote", "bullet", "numbullet"].map( (name) => { - const active = style.includes(name) || blockType === name; + const active = marks.includes(name) || blockType === name; const suffix = active ? '-o-n' : ''; const onFormatButtonClicked = this.onFormatButtonClicked.bind(this, name); const className = 'mx_MessageComposer_format_button mx_filterFlipColor'; diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 756c9eb1a9..025e42be1a 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -184,23 +184,6 @@ export default class MessageComposerInput extends React.Component { this.direction = ''; } -/* - findPillEntities(contentState: ContentState, contentBlock: ContentBlock, callback) { - contentBlock.findEntityRanges( - (character) => { - const entityKey = character.getEntity(); - return ( - entityKey !== null && - ( - contentState.getEntity(entityKey).getType() === 'LINK' || - contentState.getEntity(entityKey).getType() === ENTITY_TYPES.AT_ROOM_PILL - ) - ); - }, callback, - ); - } -*/ - /* * "Does the right thing" to create an Editor value, based on: * - whether we've got rich text mode enabled @@ -226,14 +209,12 @@ export default class MessageComposerInput extends React.Component { } componentWillUpdate(nextProps, nextState) { -/* // this is dirty, but moving all this state to MessageComposer is dirtier if (this.props.onInputStateChanged && nextState !== this.state) { const state = this.getSelectionInfo(nextState.editorState); state.isRichtextEnabled = nextState.isRichtextEnabled; this.props.onInputStateChanged(state); } -*/ } onAction = (payload) => { @@ -375,6 +356,19 @@ export default class MessageComposerInput extends React.Component { } } + if (editorState.document.getFirstText().text !== '') { + this.onTypingActivity(); + } else { + this.onFinishedTyping(); + } + + /* + // XXX: what was this ever doing? + if (!state.hasOwnProperty('originalEditorState')) { + state.originalEditorState = null; + } + */ + /* editorState = RichText.attachImmutableEntitiesToEmoji(editorState); @@ -405,6 +399,10 @@ export default class MessageComposerInput extends React.Component { // Reset selection editorState = EditorState.forceSelection(editorState, currentSelection); } +*/ + + const text = editorState.startText.text; + const currentStartOffset = editorState.startOffset; // Automatic replacement of plaintext emoji to Unicode emoji if (SettingsStore.getValue('MessageComposerInput.autoReplaceEmoji')) { @@ -415,23 +413,26 @@ export default class MessageComposerInput extends React.Component { const emojiUc = asciiList[emojiMatch[1]]; // hex unicode -> shortname -> actual unicode const unicodeEmoji = shortnameToUnicode(EMOJI_UNICODE_TO_SHORTNAME[emojiUc]); - const newContentState = Modifier.replaceText( - editorState.getCurrentContent(), - currentSelection.merge({ - anchorOffset: currentStartOffset - emojiMatch[1].length - 1, - focusOffset: currentStartOffset, - }), - unicodeEmoji, - ); - editorState = EditorState.push( - editorState, - newContentState, - 'insert-characters', - ); - editorState = EditorState.forceSelection(editorState, newContentState.getSelectionAfter()); + + const range = Range.create({ + anchorKey: editorState.selection.startKey, + anchorOffset: currentStartOffset - emojiMatch[1].length - 1, + focusKey: editorState.selection.startKey, + focusOffset: currentStartOffset, + }); + change = change.insertTextAtRange(range, unicodeEmoji); + editorState = change.value; } } -*/ + + // Record the editor state for this room so that it can be retrieved after + // switching to another room and back + dis.dispatch({ + action: 'editor_state', + room_id: this.props.room.roomId, + editor_state: editorState, + }); + /* Since a modification was made, set originalEditorState to null, since newState is now our original */ this.setState({ editorState, @@ -439,68 +440,6 @@ export default class MessageComposerInput extends React.Component { }); }; - /** - * We're overriding setState here because it's the most convenient way to monitor changes to the editorState. - * Doing it using a separate function that calls setState is a possibility (and was the old approach), but that - * approach requires a callback and an extra setState whenever trying to set multiple state properties. - * - * @param state - * @param callback - */ - setState(state, callback) { -/* - if (state.editorState != null) { - state.editorState = RichText.attachImmutableEntitiesToEmoji( - state.editorState); - - // Hide the autocomplete if the cursor location changes but the plaintext - // content stays the same. We don't hide if the pt has changed because the - // autocomplete will probably have different completions to show. - if ( - !state.editorState.getSelection().equals( - this.state.editorState.getSelection(), - ) - && state.editorState.getCurrentContent().getPlainText() === - this.state.editorState.getCurrentContent().getPlainText() - ) { - this.autocomplete.hide(); - } - - if (state.editorState.getCurrentContent().hasText()) { - this.onTypingActivity(); - } else { - this.onFinishedTyping(); - } - - // Record the editor state for this room so that it can be retrieved after - // switching to another room and back - dis.dispatch({ - action: 'editor_state', - room_id: this.props.room.roomId, - editor_state: state.editorState.getCurrentContent(), - }); - - if (!state.hasOwnProperty('originalEditorState')) { - state.originalEditorState = null; - } - } -*/ - super.setState(state, () => { - if (callback != null) { - callback(); - } -/* - const textContent = this.state.editorState.getCurrentContent().getPlainText(); - const selection = RichText.selectionStateToTextOffsets( - this.state.editorState.getSelection(), - this.state.editorState.getCurrentContent().getBlocksAsArray()); - if (this.props.onContentChanged) { - this.props.onContentChanged(textContent, selection); - } -*/ - }); - } - enableRichtext(enabled: boolean) { if (enabled === this.state.isRichtextEnabled) return; @@ -1133,36 +1072,12 @@ export default class MessageComposerInput extends React.Component { /* returns inline style and block type of current SelectionState so MessageComposer can render formatting buttons. */ getSelectionInfo(editorState: Value) { - return {}; -/* - const styleName = { - BOLD: _td('bold'), - ITALIC: _td('italic'), - STRIKETHROUGH: _td('strike'), - UNDERLINE: _td('underline'), - }; - - const originalStyle = editorState.getCurrentInlineStyle().toArray(); - const style = originalStyle - .map((style) => styleName[style] || null) - .filter((styleName) => !!styleName); - - const blockName = { - 'code-block': _td('code'), - 'blockquote': _td('quote'), - 'unordered-list-item': _td('bullet'), - 'ordered-list-item': _td('numbullet'), - }; - const originalBlockType = editorState.getCurrentContent() - .getBlockForKey(editorState.getSelection().getStartKey()) - .getType(); - const blockType = blockName[originalBlockType] || null; - return { - style, - blockType, + marks: editorState.activeMarks, + // XXX: shouldn't we return all the types of blocks in the current selection, + // not just the anchor? + blockType: editorState.anchorBlock.type, }; -*/ } getAutocompleteQuery(editorState: Value) { From 7ecb4e3b188890946712cca2580c7904ddadcb4a Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 13 May 2018 23:35:39 +0100 Subject: [PATCH 026/123] remove dead removeMDLinks code --- .../views/rooms/MessageComposerInput.js | 40 ------------------- 1 file changed, 40 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 025e42be1a..4a9dfa4b4c 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1088,9 +1088,6 @@ export default class MessageComposerInput extends React.Component { // selection offsets in & out of the plaintext domain. return editorState.document.getDescendant(editorState.selection.anchorKey).text; - - // Don't send markdown links to the autocompleter - // return this.removeMDLinks(contentState, ['@', '#']); } getSelectionRange(editorState: Value) { @@ -1119,43 +1116,6 @@ export default class MessageComposerInput extends React.Component { return range; } -/* - // delinkifies any matrix.to markdown links (i.e. pills) of form - // [#foo:matrix.org](https://matrix.to/#/#foo:matrix.org). - // the prefixes is an array of sigils that will be matched on. - removeMDLinks(contentState: ContentState, prefixes: string[]) { - const plaintext = contentState.getPlainText(); - if (!plaintext) return ''; - return plaintext.replace(REGEX_MATRIXTO_MARKDOWN_GLOBAL, - (markdownLink, text, resource, prefix, offset) => { - if (!prefixes.includes(prefix)) return markdownLink; - // Calculate the offset relative to the current block that the offset is in - let sum = 0; - const blocks = contentState.getBlocksAsArray(); - let block; - for (let i = 0; i < blocks.length; i++) { - block = blocks[i]; - sum += block.getLength(); - if (sum > offset) { - sum -= block.getLength(); - break; - } - } - offset -= sum; - - const entityKey = block.getEntityAt(offset); - const entity = entityKey ? contentState.getEntity(entityKey) : null; - if (entity && entity.getData().isCompletion) { - // This is a completed mention, so do not insert MD link, just text - return text; - } else { - // This is either a MD link that was typed into the composer or another - // type of pill (e.g. room pill) - return markdownLink; - } - }); - } -*/ onMarkdownToggleClicked = (e) => { e.preventDefault(); // don't steal focus from the editor! this.handleKeyCommand('toggle-mode'); From b10f9a9cb780a209b6e64c9b2343571eff7a93bb Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 14 May 2018 02:54:55 +0100 Subject: [PATCH 027/123] remove spurious vendor prefixing --- res/css/structures/_RoomView.scss | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/res/css/structures/_RoomView.scss b/res/css/structures/_RoomView.scss index b8e1190375..02418f70db 100644 --- a/res/css/structures/_RoomView.scss +++ b/res/css/structures/_RoomView.scss @@ -176,10 +176,7 @@ hr.mx_RoomView_myReadMarker { z-index: 1000; overflow: hidden; - -webkit-transition: all .2s ease-out; - -moz-transition: all .2s ease-out; - -ms-transition: all .2s ease-out; - -o-transition: all .2s ease-out; + transition: all .2s ease-out; } .mx_RoomView_statusArea_expanded { From c1000a7cd5aaecded59d0ffa0456012af4e15e8d Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 14 May 2018 03:02:12 +0100 Subject: [PATCH 028/123] emojioneify the composer and also fix up the selectedness CSS for pills and emoji --- res/css/_common.scss | 4 + res/css/views/elements/_RichText.scss | 4 + src/RichText.js | 81 +---------------- src/autocomplete/PlainWithPillsSerializer.js | 3 + src/components/views/elements/Pill.js | 3 + .../views/rooms/MessageComposerInput.js | 91 +++++++++++++++---- 6 files changed, 88 insertions(+), 98 deletions(-) diff --git a/res/css/_common.scss b/res/css/_common.scss index c4cda6821e..38f576a532 100644 --- a/res/css/_common.scss +++ b/res/css/_common.scss @@ -291,6 +291,10 @@ textarea { vertical-align: middle; } +.mx_emojione_selected { + background-color: $accent-color; +} + ::-moz-selection { background-color: $accent-color; color: $selection-fg-color; diff --git a/res/css/views/elements/_RichText.scss b/res/css/views/elements/_RichText.scss index 474a123455..5c390af30a 100644 --- a/res/css/views/elements/_RichText.scss +++ b/res/css/views/elements/_RichText.scss @@ -25,6 +25,10 @@ padding-right: 5px; } +.mx_UserPill_selected { + background-color: $accent-color ! important; +} + .mx_EventTile_highlight .mx_EventTile_content .markdown-body a.mx_UserPill_me, .mx_EventTile_content .mx_AtRoomPill, .mx_MessageComposer_input .mx_AtRoomPill { diff --git a/src/RichText.js b/src/RichText.js index d867636dc9..50ed33d803 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -51,10 +51,6 @@ const MARKDOWN_REGEX = { STRIKETHROUGH: /~{2}[^~]*~{2}/g, }; -const USERNAME_REGEX = /@\S+:\S+/g; -const ROOM_REGEX = /#\S+:\S+/g; -const EMOJI_REGEX = new RegExp(emojione.unicodeRegexp, 'g'); - const ZWS_CODE = 8203; const ZWS = String.fromCharCode(ZWS_CODE); // zero width space @@ -73,7 +69,7 @@ export function htmlToEditorState(html: string): Value { return Html.serialize(html); } -function unicodeToEmojiUri(str) { +export function unicodeToEmojiUri(str) { let replaceWith, unicode, alt; if ((!emojione.unicodeAlt) || (emojione.sprites)) { // if we are using the shortname as the alt tag then we need a reversed array to map unicode code point to shortnames @@ -113,27 +109,6 @@ function findWithRegex(regex, contentBlock: ContentBlock, callback: (start: numb } } -// Workaround for https://github.com/facebook/draft-js/issues/414 -const emojiDecorator = { - strategy: (contentState, contentBlock, callback) => { - findWithRegex(EMOJI_REGEX, contentBlock, callback); - }, - component: (props) => { - const uri = unicodeToEmojiUri(props.children[0].props.text); - const shortname = emojione.toShort(props.children[0].props.text); - const style = { - display: 'inline-block', - width: '1em', - maxHeight: '1em', - background: `url(${uri})`, - backgroundSize: 'contain', - backgroundPosition: 'center center', - overflow: 'hidden', - }; - return ({ props.children }); - }, -}; - /** * Returns a composite decorator which has access to provided scope. */ @@ -223,60 +198,6 @@ export function selectionStateToTextOffsets(selectionState: SelectionState, }; } -// modified version of https://github.com/draft-js-plugins/draft-js-plugins/blob/master/draft-js-emoji-plugin/src/modifiers/attachImmutableEntitiesToEmojis.js -export function attachImmutableEntitiesToEmoji(editorState: EditorState): EditorState { - const contentState = editorState.getCurrentContent(); - const blocks = contentState.getBlockMap(); - let newContentState = contentState; - - blocks.forEach((block) => { - const plainText = block.getText(); - - const addEntityToEmoji = (start, end) => { - const existingEntityKey = block.getEntityAt(start); - if (existingEntityKey) { - // avoid manipulation in case the emoji already has an entity - const entity = newContentState.getEntity(existingEntityKey); - if (entity && entity.get('type') === 'emoji') { - return; - } - } - - const selection = SelectionState.createEmpty(block.getKey()) - .set('anchorOffset', start) - .set('focusOffset', end); - const emojiText = plainText.substring(start, end); - newContentState = newContentState.createEntity( - 'emoji', 'IMMUTABLE', { emojiUnicode: emojiText }, - ); - const entityKey = newContentState.getLastCreatedEntityKey(); - newContentState = Modifier.replaceText( - newContentState, - selection, - emojiText, - null, - entityKey, - ); - }; - - findWithRegex(EMOJI_REGEX, block, addEntityToEmoji); - }); - - if (!newContentState.equals(contentState)) { - const oldSelection = editorState.getSelection(); - editorState = EditorState.push( - editorState, - newContentState, - 'convert-to-immutable-emojis', - ); - // this is somewhat of a hack, we're undoing selection changes caused above - // it would be better not to make those changes in the first place - editorState = EditorState.forceSelection(editorState, oldSelection); - } - - return editorState; -} - export function hasMultiLineSelection(editorState: EditorState): boolean { const selectionState = editorState.getSelection(); const anchorKey = selectionState.getAnchorKey(); diff --git a/src/autocomplete/PlainWithPillsSerializer.js b/src/autocomplete/PlainWithPillsSerializer.js index 6827f1fe73..0e850f2a33 100644 --- a/src/autocomplete/PlainWithPillsSerializer.js +++ b/src/autocomplete/PlainWithPillsSerializer.js @@ -61,6 +61,9 @@ class PlainWithPillsSerializer { (node.object == 'block' && Block.isBlockList(node.nodes)) ) { return node.nodes.map(this._serializeNode).join('\n'); + } + else if (node.type == 'emoji') { + return node.data.get('emojiUnicode'); } else if (node.type == 'pill') { switch (this.pillFormat) { case 'plain': diff --git a/src/components/views/elements/Pill.js b/src/components/views/elements/Pill.js index 7e5ad379de..673e4fdd03 100644 --- a/src/components/views/elements/Pill.js +++ b/src/components/views/elements/Pill.js @@ -59,6 +59,8 @@ const Pill = React.createClass({ room: PropTypes.instanceOf(Room), // Whether to include an avatar in the pill shouldShowPillAvatar: PropTypes.bool, + // Whether to render this pill as if it were highlit by a selection + isSelected: PropTypes.bool, }, @@ -233,6 +235,7 @@ const Pill = React.createClass({ const classes = classNames(pillClass, { "mx_UserPill_me": userId === MatrixClientPeg.get().credentials.userId, + "mx_UserPill_selected": this.props.isSelected, }); if (this.state.pillType) { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 4a9dfa4b4c..e682d28ff6 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -58,7 +58,7 @@ import {MATRIXTO_URL_PATTERN, MATRIXTO_MD_LINK_PATTERN} from '../../../linkify-m const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN); const REGEX_MATRIXTO_MARKDOWN_GLOBAL = new RegExp(MATRIXTO_MD_LINK_PATTERN, 'g'); -import {asciiRegexp, shortnameToUnicode, emojioneList, asciiList, mapUnicodeToShort} from 'emojione'; +import {asciiRegexp, unicodeRegexp, shortnameToUnicode, emojioneList, asciiList, mapUnicodeToShort, toShort} from 'emojione'; import SettingsStore, {SettingLevel} from "../../../settings/SettingsStore"; import {makeUserPermalink} from "../../../matrix-to"; import ReplyPreview from "./ReplyPreview"; @@ -69,6 +69,7 @@ import {ContentHelpers} from 'matrix-js-sdk'; const EMOJI_SHORTNAMES = Object.keys(emojioneList); const EMOJI_UNICODE_TO_SHORTNAME = mapUnicodeToShort(); const REGEX_EMOJI_WHITESPACE = new RegExp('(?:^|\\s)(' + asciiRegexp + ')\\s$'); +const EMOJI_REGEX = new RegExp(unicodeRegexp, 'g'); const TYPING_USER_TIMEOUT = 10000, TYPING_SERVER_TIMEOUT = 30000; @@ -76,6 +77,7 @@ const ENTITY_TYPES = { AT_ROOM_PILL: 'ATROOMPILL', }; + function onSendMessageFailed(err, room) { // XXX: temporary logging to try to diagnose // https://github.com/vector-im/riot-web/issues/3148 @@ -351,12 +353,20 @@ export default class MessageComposerInput extends React.Component { if (this.direction !== '') { const focusedNode = editorState.focusInline || editorState.focusText; if (focusedNode.isVoid) { - change = change[`collapseToEndOf${ this.direction }Text`](); + if (editorState.isCollapsed) { + change = change[`collapseToEndOf${ this.direction }Text`](); + } + else { + const block = this.direction === 'Previous' ? editorState.previousText : editorState.nextText; + if (block) { + change = change.moveFocusToEndOf(block) + } + } editorState = change.value; } } - if (editorState.document.getFirstText().text !== '') { + if (!editorState.document.isEmpty) { this.onTypingActivity(); } else { this.onFinishedTyping(); @@ -369,9 +379,33 @@ export default class MessageComposerInput extends React.Component { } */ -/* - editorState = RichText.attachImmutableEntitiesToEmoji(editorState); + // emojioneify any emoji + // deliberately lose any inlines and pills via Plain.serialize as we know + // they won't contain emoji + // XXX: is getTextsAsArray a private API? + editorState.document.getTextsAsArray().forEach(node => { + if (node.text !== '' && HtmlUtils.containsEmoji(node.text)) { + let match; + while ((match = EMOJI_REGEX.exec(node.text)) !== null) { + const range = Range.create({ + anchorKey: node.key, + anchorOffset: match.index, + focusKey: node.key, + focusOffset: match.index + match[0].length, + }); + const inline = Inline.create({ + type: 'emoji', + data: { emojiUnicode: match[0] }, + isVoid: true, + }); + change = change.insertInlineAtRange(range, inline); + editorState = change.value; + } + } + }); + +/* const currentBlock = editorState.getSelection().getStartKey(); const currentSelection = editorState.getSelection(); const currentStartOffset = editorState.getSelection().getStartOffset(); @@ -400,7 +434,6 @@ export default class MessageComposerInput extends React.Component { editorState = EditorState.forceSelection(editorState, currentSelection); } */ - const text = editorState.startText.text; const currentStartOffset = editorState.startOffset; @@ -912,8 +945,12 @@ export default class MessageComposerInput extends React.Component { // Move selection to the end of the selected history const change = editorState.change().collapseToEndOf(editorState.document); + // XXX: should we be calling this.onChange(change) now? - // we skip it for now given we know we're about to setState anyway + // Answer: yes, if we want it to do any of the fixups on stuff like emoji. + // however, this should already have been done and persisted in the history, + // so shouldn't be necessary. + editorState = change.value; this.suppressAutoComplete = true; @@ -991,11 +1028,6 @@ export default class MessageComposerInput extends React.Component { // we can't put text in here otherwise the editor tries to select it isVoid: true, }); - } else { - inline = Inline.create({ - type: 'autocompletion', - nodes: [Text.create(completion)] - }); } let editorState = activeEditorState; @@ -1007,13 +1039,23 @@ export default class MessageComposerInput extends React.Component { editorState = change.value; } - const change = editorState.change() - .insertInlineAtRange(editorState.selection, inline) - .insertText(suffix); + let change; + if (inline) { + change = editorState.change() + .insertInlineAtRange(editorState.selection, inline) + .insertText(suffix); + } + else { + change = editorState.change() + .insertTextAtRange(editorState.selection, completion) + .insertText(suffix); + } editorState = change.value; - this.setState({ editorState, originalEditorState: activeEditorState }, ()=>{ -// this.refs.editor.focus(); + this.onChange(change); + + this.setState({ + originalEditorState: activeEditorState }); return true; @@ -1027,7 +1069,7 @@ export default class MessageComposerInput extends React.Component { return

{children}

} case 'pill': { - const { data, text } = node; + const { data } = node; const url = data.get('url'); const completion = data.get('completion'); @@ -1039,6 +1081,7 @@ export default class MessageComposerInput extends React.Component { type={Pill.TYPE_AT_ROOM_MENTION} room={this.props.room} shouldShowPillAvatar={shouldShowPillAvatar} + isSelected={isSelected} />; } else if (Pill.isPillUrl(url)) { @@ -1046,14 +1089,26 @@ export default class MessageComposerInput extends React.Component { url={url} room={this.props.room} shouldShowPillAvatar={shouldShowPillAvatar} + isSelected={isSelected} />; } else { + const { text } = node; return { text } ; } } + case 'emoji': { + const { data } = node; + const emojiUnicode = data.get('emojiUnicode'); + const uri = RichText.unicodeToEmojiUri(emojiUnicode); + const shortname = toShort(emojiUnicode); + const className = classNames('mx_emojione', { + mx_emojione_selected: isSelected + }); + return {; + } } }; From 12a56e8b8e13e9d49c191605a6fb5591566e17d6 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Tue, 15 May 2018 00:59:55 +0100 Subject: [PATCH 029/123] remove spurious comment --- src/components/views/rooms/MessageComposerInput.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index e682d28ff6..204ad524fe 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -381,8 +381,6 @@ export default class MessageComposerInput extends React.Component { // emojioneify any emoji - // deliberately lose any inlines and pills via Plain.serialize as we know - // they won't contain emoji // XXX: is getTextsAsArray a private API? editorState.document.getTextsAsArray().forEach(node => { if (node.text !== '' && HtmlUtils.containsEmoji(node.text)) { From 4eb6942211e613a11c999d1c44f38691d47fd49a Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Tue, 15 May 2018 01:16:06 +0100 Subject: [PATCH 030/123] let onChange set originalEditorState --- src/components/views/rooms/MessageComposerInput.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 204ad524fe..39d49ecf83 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -346,7 +346,7 @@ export default class MessageComposerInput extends React.Component { } } - onChange = (change: Change) => { + onChange = (change: Change, originalEditorState: value) => { let editorState = change.value; @@ -467,7 +467,7 @@ export default class MessageComposerInput extends React.Component { /* Since a modification was made, set originalEditorState to null, since newState is now our original */ this.setState({ editorState, - originalEditorState: null, + originalEditorState: originalEditorState || null }); }; @@ -1050,11 +1050,7 @@ export default class MessageComposerInput extends React.Component { } editorState = change.value; - this.onChange(change); - - this.setState({ - originalEditorState: activeEditorState - }); + this.onChange(change, activeEditorState); return true; }; From ae208da805f693cf016655834f267ecf9846c22c Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Thu, 17 May 2018 00:01:23 +0100 Subject: [PATCH 031/123] nudge towards supporting formatting buttons in MD --- .../views/rooms/MessageComposerInput.js | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 39d49ecf83..1a2450696c 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -536,11 +536,12 @@ export default class MessageComposerInput extends React.Component { this.enableRichtext(!this.state.isRichtextEnabled); return true; } -/* - let newState: ?EditorState = null; + + let newState: ?Value = null; // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown. if (this.state.isRichtextEnabled) { +/* // These are block types, not handled by RichUtils by default. const blockCommands = ['code-block', 'blockquote', 'unordered-list-item', 'ordered-list-item']; const currentBlockType = RichUtils.getCurrentBlockType(this.state.editorState); @@ -563,7 +564,9 @@ export default class MessageComposerInput extends React.Component { newState = RichUtils.toggleBlockType(this.state.editorState, currentBlockType); } } +*/ } else { +/* const contentState = this.state.editorState.getCurrentContent(); const multipleLinesSelected = RichText.hasMultiLineSelection(this.state.editorState); @@ -599,16 +602,17 @@ export default class MessageComposerInput extends React.Component { 'blockquote': -2, }[command]; - // Returns a function that collapses a selectionState to its end and moves it by offset - const collapseAndOffsetSelection = (selectionState, offset) => { - const key = selectionState.getEndKey(); - return new SelectionState({ + // Returns a function that collapses a selection to its end and moves it by offset + const collapseAndOffsetSelection = (selection, offset) => { + const key = selection.endKey(); + return new Range({ anchorKey: key, anchorOffset: offset, focusKey: key, focusOffset: offset, }); }; if (modifyFn) { + const previousSelection = this.state.editorState.getSelection(); const newContentState = RichText.modifyText(contentState, previousSelection, modifyFn); newState = EditorState.push( @@ -633,15 +637,11 @@ export default class MessageComposerInput extends React.Component { } } - if (newState == null) { - newState = RichUtils.handleKeyCommand(this.state.editorState, command); - } - if (newState != null) { this.setState({editorState: newState}); return true; } -*/ +*/ return false; }; /* From e51554c626429c3ce42a100824d56762670446c6 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Thu, 17 May 2018 02:13:17 +0100 Subject: [PATCH 032/123] actually hook up RTE --- src/ComposerHistoryManager.js | 2 +- .../views/rooms/MessageComposerInput.js | 295 +++++++++++------- 2 files changed, 189 insertions(+), 108 deletions(-) diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index ce0eb8f0c3..28749ace15 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -17,7 +17,7 @@ limitations under the License. import { Value } from 'slate'; import Html from 'slate-html-serializer'; -import { Markdown as Md } from 'slate-md-serializer'; +import Md from 'slate-md-serializer'; import Plain from 'slate-plain-serializer'; import * as RichText from './RichText'; import Markdown from './Markdown'; diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 1a2450696c..70a9b9bd83 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -23,7 +23,7 @@ import { Editor } from 'slate-react'; import { Value, Document, Event, Inline, Text, Range, Node } from 'slate'; import Html from 'slate-html-serializer'; -import { Markdown as Md } from 'slate-md-serializer'; +import Md from 'slate-md-serializer'; import Plain from 'slate-plain-serializer'; import PlainWithPillsSerializer from "../../../autocomplete/PlainWithPillsSerializer"; @@ -107,43 +107,6 @@ export default class MessageComposerInput extends React.Component { onInputStateChanged: PropTypes.func, }; -/* - static getKeyBinding(ev: SyntheticKeyboardEvent): string { - // Restrict a subset of key bindings to ONLY having ctrl/meta* pressed and - // importantly NOT having alt, shift, meta/ctrl* pressed. draft-js does not - // handle this in `getDefaultKeyBinding` so we do it ourselves here. - // - // * if macOS, read second option - const ctrlCmdCommand = { - // C-m => Toggles between rich text and markdown modes - [KeyCode.KEY_M]: 'toggle-mode', - [KeyCode.KEY_B]: 'bold', - [KeyCode.KEY_I]: 'italic', - [KeyCode.KEY_U]: 'underline', - [KeyCode.KEY_J]: 'code', - [KeyCode.KEY_O]: 'split-block', - }[ev.keyCode]; - - if (ctrlCmdCommand) { - if (!isOnlyCtrlOrCmdKeyEvent(ev)) { - return null; - } - return ctrlCmdCommand; - } - - // Handle keys such as return, left and right arrows etc. - return getDefaultKeyBinding(ev); - } - - static getBlockStyle(block: ContentBlock): ?string { - if (block.getType() === 'strikethrough') { - return 'mx_Markdown_STRIKETHROUGH'; - } - - return null; - } -*/ - client: MatrixClient; autocomplete: Autocomplete; historyManager: ComposerHistoryManager; @@ -181,6 +144,8 @@ export default class MessageComposerInput extends React.Component { this.plainWithMdPills = new PlainWithPillsSerializer({ pillFormat: 'md' }); this.plainWithIdPills = new PlainWithPillsSerializer({ pillFormat: 'id' }); this.plainWithPlainPills = new PlainWithPillsSerializer({ pillFormat: 'plain' }); + this.md = new Md(); + this.html = new Html(); this.suppressAutoComplete = false; this.direction = ''; @@ -191,9 +156,9 @@ export default class MessageComposerInput extends React.Component { * - whether we've got rich text mode enabled * - contentState was passed in */ - createEditorState(richText: boolean, value: ?Value): Value { - if (value instanceof Value) { - return value; + createEditorState(richText: boolean, editorState: ?Value): Value { + if (editorState instanceof Value) { + return editorState; } else { // ...or create a new one. @@ -275,7 +240,7 @@ export default class MessageComposerInput extends React.Component { } } break; -*/ +*/ } }; @@ -403,7 +368,7 @@ export default class MessageComposerInput extends React.Component { } }); -/* +/* const currentBlock = editorState.getSelection().getStartKey(); const currentSelection = editorState.getSelection(); const currentStartOffset = editorState.getSelection().getStartOffset(); @@ -477,27 +442,54 @@ export default class MessageComposerInput extends React.Component { // FIXME: this conversion should be handled in the store, surely // i.e. "convert my current composer value into Rich or MD, as ComposerHistoryManager already does" - let value = null; + let editorState = null; if (enabled) { - // const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText()); - // contentState = RichText.htmlToContentState(md.toHTML()); + // const sourceWithPills = this.plainWithMdPills.serialize(this.state.editorState); + // const markdown = new Markdown(sourceWithPills); + // editorState = this.html.deserialize(markdown.toHTML()); - value = Md.deserialize(Plain.serialize(this.state.editorState)); + // we don't really want a custom MD parser hanging around, but the + // alternative would be: + editorState = this.md.deserialize(this.plainWithMdPills.serialize(this.state.editorState)); } else { // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); // value = ContentState.createFromText(markdown); - value = Plain.deserialize(Md.serialize(this.state.editorState)); + editorState = Plain.deserialize(this.md.serialize(this.state.editorState)); } Analytics.setRichtextMode(enabled); this.setState({ - editorState: this.createEditorState(enabled, value), + editorState: this.createEditorState(enabled, editorState), isRichtextEnabled: enabled, }); SettingsStore.setValue("MessageComposerInput.isRichTextEnabled", null, SettingLevel.ACCOUNT, enabled); - } + }; + + /** + * Check if the current selection has a mark with `type` in it. + * + * @param {String} type + * @return {Boolean} + */ + + hasMark = type => { + const { editorState } = this.state + return editorState.activeMarks.some(mark => mark.type == type) + }; + + /** + * Check if the any of the currently selected blocks are of `type`. + * + * @param {String} type + * @return {Boolean} + */ + + hasBlock = type => { + const { editorState } = this.state + return editorState.blocks.some(node => node.type == type) + }; onKeyDown = (ev: Event, change: Change, editor: Editor) => { @@ -514,6 +506,22 @@ export default class MessageComposerInput extends React.Component { this.direction = ''; } + if (isOnlyCtrlOrCmdKeyEvent(ev)) { + const ctrlCmdCommand = { + // C-m => Toggles between rich text and markdown modes + [KeyCode.KEY_M]: 'toggle-mode', + [KeyCode.KEY_B]: 'bold', + [KeyCode.KEY_I]: 'italic', + [KeyCode.KEY_U]: 'underline', + [KeyCode.KEY_J]: 'code', + }[ev.keyCode]; + + if (ctrlCmdCommand) { + return this.handleKeyCommand(ctrlCmdCommand); + } + return false; + } + switch (ev.keyCode) { case KeyCode.ENTER: return this.handleReturn(ev); @@ -529,7 +537,7 @@ export default class MessageComposerInput extends React.Component { // don't intercept it return; } - } + }; handleKeyCommand = (command: string): boolean => { if (command === 'toggle-mode') { @@ -541,32 +549,79 @@ export default class MessageComposerInput extends React.Component { // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown. if (this.state.isRichtextEnabled) { -/* - // These are block types, not handled by RichUtils by default. - const blockCommands = ['code-block', 'blockquote', 'unordered-list-item', 'ordered-list-item']; - const currentBlockType = RichUtils.getCurrentBlockType(this.state.editorState); + const type = command; + const { editorState } = this.state; + const change = editorState.change(); + const { document } = editorState; + switch (type) { + // list-blocks: + case 'bulleted-list': + case 'numbered-list': { + // Handle the extra wrapping required for list buttons. + const isList = this.hasBlock('list-item'); + const isType = editorState.blocks.some(block => { + return !!document.getClosest(block.key, parent => parent.type == type); + }); - const shouldToggleBlockFormat = ( - command === 'backspace' || - command === 'split-block' - ) && currentBlockType !== 'unstyled'; - - if (blockCommands.includes(command)) { - newState = RichUtils.toggleBlockType(this.state.editorState, command); - } else if (command === 'strike') { - // this is the only inline style not handled by Draft by default - newState = RichUtils.toggleInlineStyle(this.state.editorState, 'STRIKETHROUGH'); - } else if (shouldToggleBlockFormat) { - const currentStartOffset = this.state.editorState.getSelection().getStartOffset(); - const currentEndOffset = this.state.editorState.getSelection().getEndOffset(); - if (currentStartOffset === 0 && currentEndOffset === 0) { - // Toggle current block type (setting it to 'unstyled') - newState = RichUtils.toggleBlockType(this.state.editorState, currentBlockType); + if (isList && isType) { + change + .setBlocks(DEFAULT_NODE) + .unwrapBlock('bulleted-list') + .unwrapBlock('numbered-list'); + } else if (isList) { + change + .unwrapBlock( + type == 'bulleted-list' ? 'numbered-list' : 'bulleted-list' + ) + .wrapBlock(type); + } else { + change.setBlocks('list-item').wrapBlock(type); + } } + break; + + // simple blocks + case 'paragraph': + case 'block-quote': + case 'heading-one': + case 'heading-two': + case 'heading-three': + case 'list-item': + case 'code-block': { + const isActive = this.hasBlock(type); + const isList = this.hasBlock('list-item'); + + if (isList) { + change + .setBlocks(isActive ? DEFAULT_NODE : type) + .unwrapBlock('bulleted-list') + .unwrapBlock('numbered-list'); + } else { + change.setBlocks(isActive ? DEFAULT_NODE : type); + } + } + break; + + // marks: + case 'bold': + case 'italic': + case 'code': + case 'underline': + case 'strikethrough': { + change.toggleMark(type); + } + break; + + default: + console.warn(`ignoring unrecognised RTE command ${type}`); + return false; } -*/ + + this.onChange(change); + + return true; } else { -/* +/* const contentState = this.state.editorState.getCurrentContent(); const multipleLinesSelected = RichText.hasMultiLineSelection(this.state.editorState); @@ -641,7 +696,8 @@ export default class MessageComposerInput extends React.Component { this.setState({editorState: newState}); return true; } -*/ +*/ + } return false; }; /* @@ -671,19 +727,14 @@ export default class MessageComposerInput extends React.Component { if (ev.shiftKey) { return; } -/* - const currentBlockType = RichUtils.getCurrentBlockType(this.state.editorState); - if ( - ['code-block', 'blockquote', 'unordered-list-item', 'ordered-list-item'] - .includes(currentBlockType) - ) { - // By returning false, we allow the default draft-js key binding to occur, - // which in this case invokes "split-block". This creates a new block of the - // same type, allowing the user to delete it with backspace. - // See handleKeyCommand (when command === 'backspace') - return false; + + if (this.state.editorState.blocks.some( + block => block in ['code-block', 'block-quote', 'bulleted-list', 'numbered-list'] + )) { + // allow the user to terminate blocks by hitting return rather than sending a msg + return; } -*/ + const editorState = this.state.editorState; let contentText; @@ -989,6 +1040,17 @@ export default class MessageComposerInput extends React.Component { await this.setDisplayedCompletion(null); // restore originalEditorState }; + /* returns inline style and block type of current SelectionState so MessageComposer can render formatting + buttons. */ + getSelectionInfo(editorState: Value) { + return { + marks: editorState.activeMarks, + // XXX: shouldn't we return all the types of blocks in the current selection, + // not just the anchor? + blockType: editorState.anchorBlock.type, + }; + } + /* If passed null, restores the original editor content from state.originalEditorState. * If passed a non-null displayedCompletion, modifies state.originalEditorState to compute new state.editorState. */ @@ -1059,9 +1121,24 @@ export default class MessageComposerInput extends React.Component { const { attributes, children, node, isSelected } = props; switch (node.type) { - case 'paragraph': { - return

{children}

- } + case 'paragraph': + return

{children}

; + case 'block-quote': + return
{children}
; + case 'bulleted-list': + return
    {children}
; + case 'heading-one': + return

{children}

; + case 'heading-two': + return

{children}

; + case 'heading-three': + return

{children}

; + case 'list-item': + return
  • {children}
  • ; + case 'numbered-list': + return
      {children}
    ; + case 'code-block': + return

    {children}

    ; case 'pill': { const { data } = node; const url = data.get('url'); @@ -1106,29 +1183,35 @@ export default class MessageComposerInput extends React.Component { } }; + renderMark = props => { + const { children, mark, attributes } = props; + switch (mark.type) { + case 'bold': + return {children}; + case 'italic': + return {children}; + case 'code': + return {children}; + case 'underline': + return {children}; + case 'strikethrough': + return {children}; + } + }; + onFormatButtonClicked = (name: "bold" | "italic" | "strike" | "code" | "underline" | "quote" | "bullet" | "numbullet", e) => { e.preventDefault(); // don't steal focus from the editor! const command = { code: 'code-block', - quote: 'blockquote', - bullet: 'unordered-list-item', - numbullet: 'ordered-list-item', + quote: 'block-quote', + bullet: 'bulleted-list', + numbullet: 'numbered-list', + strike: 'strike-through', }[name] || name; this.handleKeyCommand(command); }; - /* returns inline style and block type of current SelectionState so MessageComposer can render formatting - buttons. */ - getSelectionInfo(editorState: Value) { - return { - marks: editorState.activeMarks, - // XXX: shouldn't we return all the types of blocks in the current selection, - // not just the anchor? - blockType: editorState.anchorBlock.type, - }; - } - getAutocompleteQuery(editorState: Value) { // We can just return the current block where the selection begins, which // should be enough to capture any autocompletion input, given autocompletion @@ -1154,7 +1237,7 @@ export default class MessageComposerInput extends React.Component { const range = { beginning, // whether the selection is in the first block of the editor or not start: editorState.selection.anchorOffset, - end: (editorState.selection.anchorKey == editorState.selection.focusKey) ? + end: (editorState.selection.anchorKey == editorState.selection.focusKey) ? editorState.selection.focusOffset : editorState.selection.anchorOffset, } if (range.start > range.end) { @@ -1203,11 +1286,9 @@ export default class MessageComposerInput extends React.Component { onChange={this.onChange} onKeyDown={this.onKeyDown} renderNode={this.renderNode} + renderMark={this.renderMark} spellCheck={true} /* - blockStyleFn={MessageComposerInput.getBlockStyle} - keyBindingFn={MessageComposerInput.getKeyBinding} - handleKeyCommand={this.handleKeyCommand} handlePastedText={this.onTextPasted} handlePastedFiles={this.props.onFilesPasted} stripPastedStyles={!this.state.isRichtextEnabled} From 089ac337f4125c34823709afabd4b9788728e740 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Fri, 18 May 2018 15:22:24 +0100 Subject: [PATCH 033/123] remove unused html serializer --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 70a9b9bd83..acd7c0fab3 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -145,7 +145,7 @@ export default class MessageComposerInput extends React.Component { this.plainWithIdPills = new PlainWithPillsSerializer({ pillFormat: 'id' }); this.plainWithPlainPills = new PlainWithPillsSerializer({ pillFormat: 'plain' }); this.md = new Md(); - this.html = new Html(); + //this.html = new Html(); // not used atm this.suppressAutoComplete = false; this.direction = ''; From 167742d9008a7588c1c3bf044563db1fcc8e9816 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 20:28:38 +0100 Subject: [PATCH 034/123] make RTE sending work --- res/css/views/rooms/_MessageComposer.scss | 9 ++ src/RichText.js | 8 - .../views/rooms/MessageComposerInput.js | 137 ++++++++++-------- 3 files changed, 85 insertions(+), 69 deletions(-) diff --git a/res/css/views/rooms/_MessageComposer.scss b/res/css/views/rooms/_MessageComposer.scss index 14f52832f6..72d31cfddd 100644 --- a/res/css/views/rooms/_MessageComposer.scss +++ b/res/css/views/rooms/_MessageComposer.scss @@ -91,6 +91,15 @@ limitations under the License. overflow: auto; } +// FIXME: rather unpleasant hack to get rid of

    margins. +// really we should be mixing in markdown-body from gfm.css instead +.mx_MessageComposer_editor > :first-child { + margin-top: 0 ! important; +} +.mx_MessageComposer_editor > :last-child { + margin-bottom: 0 ! important; +} + @keyframes visualbell { from { background-color: #faa } diff --git a/src/RichText.js b/src/RichText.js index 50ed33d803..e3162a4e2c 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -61,14 +61,6 @@ export function stateToMarkdown(state) { ''); // this is *not* a zero width space, trust me :) } -export const editorStateToHTML = (editorState: Value) => { - return Html.deserialize(editorState); -} - -export function htmlToEditorState(html: string): Value { - return Html.serialize(html); -} - export function unicodeToEmojiUri(str) { let replaceWith, unicode, alt; if ((!emojione.unicodeAlt) || (emojione.sprites)) { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index acd7c0fab3..ae4d5b6264 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -145,7 +145,26 @@ export default class MessageComposerInput extends React.Component { this.plainWithIdPills = new PlainWithPillsSerializer({ pillFormat: 'id' }); this.plainWithPlainPills = new PlainWithPillsSerializer({ pillFormat: 'plain' }); this.md = new Md(); - //this.html = new Html(); // not used atm + this.html = new Html({ + rules: [ + { + serialize: (obj, children) => { + if (obj.object === 'block' || obj.object === 'inline') { + return this.renderNode({ + node: obj, + children: children, + }); + } + else if (obj.object === 'mark') { + return this.renderMark({ + mark: obj, + children: children, + }); + } + } + } + ] + }); this.suppressAutoComplete = false; this.direction = ''; @@ -397,27 +416,29 @@ export default class MessageComposerInput extends React.Component { editorState = EditorState.forceSelection(editorState, currentSelection); } */ - const text = editorState.startText.text; - const currentStartOffset = editorState.startOffset; + if (editorState.startText !== null) { + const text = editorState.startText.text; + const currentStartOffset = editorState.startOffset; - // Automatic replacement of plaintext emoji to Unicode emoji - if (SettingsStore.getValue('MessageComposerInput.autoReplaceEmoji')) { - // The first matched group includes just the matched plaintext emoji - const emojiMatch = REGEX_EMOJI_WHITESPACE.exec(text.slice(0, currentStartOffset)); - if (emojiMatch) { - // plaintext -> hex unicode - const emojiUc = asciiList[emojiMatch[1]]; - // hex unicode -> shortname -> actual unicode - const unicodeEmoji = shortnameToUnicode(EMOJI_UNICODE_TO_SHORTNAME[emojiUc]); + // Automatic replacement of plaintext emoji to Unicode emoji + if (SettingsStore.getValue('MessageComposerInput.autoReplaceEmoji')) { + // The first matched group includes just the matched plaintext emoji + const emojiMatch = REGEX_EMOJI_WHITESPACE.exec(text.slice(0, currentStartOffset)); + if (emojiMatch) { + // plaintext -> hex unicode + const emojiUc = asciiList[emojiMatch[1]]; + // hex unicode -> shortname -> actual unicode + const unicodeEmoji = shortnameToUnicode(EMOJI_UNICODE_TO_SHORTNAME[emojiUc]); - const range = Range.create({ - anchorKey: editorState.selection.startKey, - anchorOffset: currentStartOffset - emojiMatch[1].length - 1, - focusKey: editorState.selection.startKey, - focusOffset: currentStartOffset, - }); - change = change.insertTextAtRange(range, unicodeEmoji); - editorState = change.value; + const range = Range.create({ + anchorKey: editorState.selection.startKey, + anchorOffset: currentStartOffset - emojiMatch[1].length - 1, + focusKey: editorState.selection.startKey, + focusOffset: currentStartOffset, + }); + change = change.insertTextAtRange(range, unicodeEmoji); + editorState = change.value; + } } } @@ -444,13 +465,15 @@ export default class MessageComposerInput extends React.Component { let editorState = null; if (enabled) { + // for simplicity when roundtripping, we use slate-md-serializer rather than commonmark + editorState = this.md.deserialize(this.plainWithMdPills.serialize(this.state.editorState)); + + // the alternative would be something like: + // // const sourceWithPills = this.plainWithMdPills.serialize(this.state.editorState); // const markdown = new Markdown(sourceWithPills); // editorState = this.html.deserialize(markdown.toHTML()); - // we don't really want a custom MD parser hanging around, but the - // alternative would be: - editorState = this.md.deserialize(this.plainWithMdPills.serialize(this.state.editorState)); } else { // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); // value = ContentState.createFromText(markdown); @@ -547,6 +570,8 @@ export default class MessageComposerInput extends React.Component { let newState: ?Value = null; + const DEFAULT_NODE = 'paragraph'; + // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown. if (this.state.isRichtextEnabled) { const type = command; @@ -725,11 +750,14 @@ export default class MessageComposerInput extends React.Component { */ handleReturn = (ev) => { if (ev.shiftKey) { + // FIXME: we should insert a
    equivalent rather than letting Slate + // split the current block, otherwise

    will be split into two paragraphs + // and it'll look like a double line-break. return; } if (this.state.editorState.blocks.some( - block => block in ['code-block', 'block-quote', 'bulleted-list', 'numbered-list'] + block => ['code-block', 'block-quote', 'list-item'].includes(block.type) )) { // allow the user to terminate blocks by hitting return rather than sending a msg return; @@ -788,47 +816,25 @@ export default class MessageComposerInput extends React.Component { const mustSendHTML = Boolean(replyingToEv); if (this.state.isRichtextEnabled) { -/* // We should only send HTML if any block is styled or contains inline style let shouldSendHTML = false; if (mustSendHTML) shouldSendHTML = true; - const blocks = contentState.getBlocksAsArray(); - if (blocks.some((block) => block.getType() !== 'unstyled')) { - shouldSendHTML = true; - } else { - const characterLists = blocks.map((block) => block.getCharacterList()); - // For each block of characters, determine if any inline styles are applied - // and if yes, send HTML - characterLists.forEach((characters) => { - const numberOfStylesForCharacters = characters.map( - (character) => character.getStyle().toArray().length, - ).toArray(); - // If any character has more than 0 inline styles applied, send HTML - if (numberOfStylesForCharacters.some((styles) => styles > 0)) { - shouldSendHTML = true; - } - }); - } if (!shouldSendHTML) { - const hasLink = blocks.some((block) => { - return block.getCharacterList().filter((c) => { - const entityKey = c.getEntity(); - return entityKey && contentState.getEntity(entityKey).getType() === 'LINK'; - }).size > 0; + shouldSendHTML = !!editorState.document.findDescendant(node => { + // N.B. node.getMarks() might be private? + return ((node.object === 'block' && node.type !== 'line') || + (node.object === 'inline') || + (node.object === 'text' && node.getMarks().size > 0)); }); - shouldSendHTML = hasLink; } -*/ + contentText = this.plainWithPlainPills.serialize(editorState); if (contentText === '') return true; - let shouldSendHTML = true; if (shouldSendHTML) { - contentHTML = HtmlUtils.processHtmlForSending( - RichText.editorStateToHTML(editorState), - ); + contentHTML = this.html.serialize(editorState); // HtmlUtils.processHtmlForSending(); } } else { const sourceWithPills = this.plainWithMdPills.serialize(editorState); @@ -1047,7 +1053,7 @@ export default class MessageComposerInput extends React.Component { marks: editorState.activeMarks, // XXX: shouldn't we return all the types of blocks in the current selection, // not just the anchor? - blockType: editorState.anchorBlock.type, + blockType: editorState.anchorBlock ? editorState.anchorBlock.type : null, }; } @@ -1121,6 +1127,10 @@ export default class MessageComposerInput extends React.Component { const { attributes, children, node, isSelected } = props; switch (node.type) { + case 'line': + // ideally we'd return { children }
    , but as this isn't + // a valid react component, we don't have much choice. + return

    {children}
    ; case 'paragraph': return

    {children}

    ; case 'block-quote': @@ -1138,7 +1148,7 @@ export default class MessageComposerInput extends React.Component { case 'numbered-list': return
      {children}
    ; case 'code-block': - return

    {children}

    ; + return
    {children}
    ; case 'pill': { const { data } = node; const url = data.get('url'); @@ -1187,15 +1197,15 @@ export default class MessageComposerInput extends React.Component { const { children, mark, attributes } = props; switch (mark.type) { case 'bold': - return {children}; + return {children}; case 'italic': - return {children}; + return {children}; case 'code': - return {children}; + return {children}; case 'underline': - return {children}; + return {children}; case 'strikethrough': - return {children}; + return {children}; } }; @@ -1219,7 +1229,12 @@ export default class MessageComposerInput extends React.Component { // This avoids us having to serialize the whole thing to plaintext and convert // selection offsets in & out of the plaintext domain. - return editorState.document.getDescendant(editorState.selection.anchorKey).text; + if (editorState.selection.anchorKey) { + return editorState.document.getDescendant(editorState.selection.anchorKey).text; + } + else { + return ''; + } } getSelectionRange(editorState: Value) { From a4d9338cf0d63fa6f637a96bbb52694f97bd791a Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 20:38:07 +0100 Subject: [PATCH 035/123] let backspace delete list nodes in RTE --- .../views/rooms/MessageComposerInput.js | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index ae4d5b6264..ab104f825a 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -77,6 +77,10 @@ const ENTITY_TYPES = { AT_ROOM_PILL: 'ATROOMPILL', }; +// the Slate node type to default to for unstyled text when in RTE mode. +// (we use 'line' for oneliners however) +const DEFAULT_NODE = 'paragraph'; + function onSendMessageFailed(err, room) { // XXX: temporary logging to try to diagnose @@ -152,13 +156,13 @@ export default class MessageComposerInput extends React.Component { if (obj.object === 'block' || obj.object === 'inline') { return this.renderNode({ node: obj, - children: children, + children: children, }); } else if (obj.object === 'mark') { return this.renderMark({ mark: obj, - children: children, + children: children, }); } } @@ -548,6 +552,8 @@ export default class MessageComposerInput extends React.Component { switch (ev.keyCode) { case KeyCode.ENTER: return this.handleReturn(ev); + case KeyCode.BACKSPACE: + return this.onBackspace(ev); case KeyCode.UP: return this.onVerticalArrow(ev, true); case KeyCode.DOWN: @@ -562,6 +568,23 @@ export default class MessageComposerInput extends React.Component { } }; + onBackspace = (ev: Event): boolean => { + if (this.state.isRichtextEnabled) { + // let backspace exit lists + const isList = this.hasBlock('list-item'); + if (isList) { + const change = this.state.editorState.change(); + change + .setBlocks(DEFAULT_NODE) + .unwrapBlock('bulleted-list') + .unwrapBlock('numbered-list'); + this.onChange(change); + return true; + } + } + return; + }; + handleKeyCommand = (command: string): boolean => { if (command === 'toggle-mode') { this.enableRichtext(!this.state.isRichtextEnabled); @@ -570,8 +593,6 @@ export default class MessageComposerInput extends React.Component { let newState: ?Value = null; - const DEFAULT_NODE = 'paragraph'; - // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown. if (this.state.isRichtextEnabled) { const type = command; From 58670cc3e54114c00f5bccf43d21440e5c3ceec8 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 21:14:39 +0100 Subject: [PATCH 036/123] exit list more sanely on backspace --- src/components/views/rooms/MessageComposerInput.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index ab104f825a..6eadbae5ec 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -572,8 +572,9 @@ export default class MessageComposerInput extends React.Component { if (this.state.isRichtextEnabled) { // let backspace exit lists const isList = this.hasBlock('list-item'); - if (isList) { - const change = this.state.editorState.change(); + const { editorState } = this.state; + if (isList && editorState.anchorOffset == 0) { + const change = editorState.change(); change .setBlocks(DEFAULT_NODE) .unwrapBlock('bulleted-list') From d426c3474f2b5703b5c1f69b5ae1313a791c8a6d Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 21:36:22 +0100 Subject: [PATCH 037/123] fix strikethough & code, improve shift-return & backspace --- .../views/rooms/MessageComposerInput.js | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 6eadbae5ec..d7884e3c83 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -551,9 +551,9 @@ export default class MessageComposerInput extends React.Component { switch (ev.keyCode) { case KeyCode.ENTER: - return this.handleReturn(ev); + return this.handleReturn(ev, change); case KeyCode.BACKSPACE: - return this.onBackspace(ev); + return this.onBackspace(ev, change); case KeyCode.UP: return this.onVerticalArrow(ev, true); case KeyCode.DOWN: @@ -568,19 +568,27 @@ export default class MessageComposerInput extends React.Component { } }; - onBackspace = (ev: Event): boolean => { + onBackspace = (ev: Event, change: Change): Change => { if (this.state.isRichtextEnabled) { // let backspace exit lists const isList = this.hasBlock('list-item'); const { editorState } = this.state; + if (isList && editorState.anchorOffset == 0) { - const change = editorState.change(); change .setBlocks(DEFAULT_NODE) .unwrapBlock('bulleted-list') .unwrapBlock('numbered-list'); - this.onChange(change); - return true; + return change; + } + else if (editorState.anchorOffset == 0 && + (this.hasBlock('block-quote') || + this.hasBlock('heading-one') || + this.hasBlock('heading-two') || + this.hasBlock('heading-three') || + this.hasBlock('code-block'))) + { + return change.setBlocks(DEFAULT_NODE); } } return; @@ -770,12 +778,13 @@ export default class MessageComposerInput extends React.Component { return true; }; */ - handleReturn = (ev) => { + handleReturn = (ev, change) => { if (ev.shiftKey) { + // FIXME: we should insert a
    equivalent rather than letting Slate // split the current block, otherwise

    will be split into two paragraphs // and it'll look like a double line-break. - return; + return change.insertText('\n'); } if (this.state.editorState.blocks.some( @@ -1235,11 +1244,11 @@ export default class MessageComposerInput extends React.Component { e.preventDefault(); // don't steal focus from the editor! const command = { - code: 'code-block', + // code: 'code-block', // let's have the button do inline code for now quote: 'block-quote', bullet: 'bulleted-list', numbullet: 'numbered-list', - strike: 'strike-through', + strike: 'strikethrough', }[name] || name; this.handleKeyCommand(command); }; From 1536ab433acf2ad5bed2eff14e9e9e8534fc2433 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 22:05:31 +0100 Subject: [PATCH 038/123] make file pasting work again --- .../views/rooms/MessageComposerInput.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index d7884e3c83..c783a7dd7f 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -20,6 +20,7 @@ import PropTypes from 'prop-types'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import { Editor } from 'slate-react'; +import { getEventTransfer } from 'slate-react'; import { Value, Document, Event, Inline, Text, Range, Node } from 'slate'; import Html from 'slate-html-serializer'; @@ -755,6 +756,18 @@ export default class MessageComposerInput extends React.Component { } return false; }; + + onPaste = (event: Event, change: Change, editor: Editor): Change => { + const transfer = getEventTransfer(event); + + if (transfer.type === "files") { + return this.props.onFilesPasted(transfer.files); + } + if (transfer.type === "html") { + + } + }; + /* onTextPasted = (text: string, html?: string) => { const currentSelection = this.state.editorState.getSelection(); @@ -1331,14 +1344,10 @@ export default class MessageComposerInput extends React.Component { value={this.state.editorState} onChange={this.onChange} onKeyDown={this.onKeyDown} + onPaste={this.onPaste} renderNode={this.renderNode} renderMark={this.renderMark} spellCheck={true} - /* - handlePastedText={this.onTextPasted} - handlePastedFiles={this.props.onFilesPasted} - stripPastedStyles={!this.state.isRichtextEnabled} - */ /> From 1f05aea884de58efda88c9c3327e8a4e4a06d594 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 23:33:07 +0100 Subject: [PATCH 039/123] make HTML pasting work --- .../views/rooms/MessageComposerInput.js | 78 +++++++++++++------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index c783a7dd7f..6c178ce078 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -82,6 +82,32 @@ const ENTITY_TYPES = { // (we use 'line' for oneliners however) const DEFAULT_NODE = 'paragraph'; +// map HTML elements through to our Slate schema node types +// used for the HTML deserializer. +// (We don't use the same names so that they are closer to the MD serializer's schema) +const BLOCK_TAGS = { + p: 'paragraph', + blockquote: 'block-quote', + ul: 'bulleted-list', + h1: 'heading-one', + h2: 'heading-two', + h3: 'heading-three', + li: 'list-item', + ol: 'numbered-list', + pre: 'code-block', +}; + +const MARK_TAGS = { + strong: 'bold', + b: 'bold', // deprecated + em: 'italic', + i: 'italic', // deprecated + code: 'code', + u: 'underline', + del: 'strikethrough', + strike: 'strikethrough', // deprecated + s: 'strikethrough', // deprecated +}; function onSendMessageFailed(err, room) { // XXX: temporary logging to try to diagnose @@ -153,6 +179,25 @@ export default class MessageComposerInput extends React.Component { this.html = new Html({ rules: [ { + deserialize: (el, next) => { + const tag = el.tagName.toLowerCase(); + let type = BLOCK_TAGS[tag]; + if (type) { + return { + object: 'block', + type: type, + nodes: next(el.childNodes), + } + } + type = MARK_TAGS[tag]; + if (type) { + return { + object: 'mark', + type: type, + nodes: next(el.childNodes), + } + } + }, serialize: (obj, children) => { if (obj.object === 'block' || obj.object === 'inline') { return this.renderNode({ @@ -763,34 +808,17 @@ export default class MessageComposerInput extends React.Component { if (transfer.type === "files") { return this.props.onFilesPasted(transfer.files); } - if (transfer.type === "html") { - + else if (transfer.type === "html") { + const fragment = this.html.deserialize(transfer.html); + if (this.state.isRichtextEnabled) { + return change.insertFragment(fragment.document); + } + else { + return change.insertText(this.md.serialize(fragment)); + } } }; -/* - onTextPasted = (text: string, html?: string) => { - const currentSelection = this.state.editorState.getSelection(); - const currentContent = this.state.editorState.getCurrentContent(); - - let contentState = null; - if (html && this.state.isRichtextEnabled) { - contentState = Modifier.replaceWithFragment( - currentContent, - currentSelection, - RichText.htmlToContentState(html).getBlockMap(), - ); - } else { - contentState = Modifier.replaceText(currentContent, currentSelection, text); - } - - let newEditorState = EditorState.push(this.state.editorState, contentState, 'insert-characters'); - - newEditorState = EditorState.forceSelection(newEditorState, contentState.getSelectionAfter()); - this.onEditorContentChanged(newEditorState); - return true; - }; -*/ handleReturn = (ev, change) => { if (ev.shiftKey) { From 572a31334fa87b6c1fc8e1b39962f0a6f823b06e Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 23:34:30 +0100 Subject: [PATCH 040/123] add h4, h5 and h6 --- .../views/rooms/MessageComposerInput.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 6c178ce078..30461ca816 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -92,6 +92,9 @@ const BLOCK_TAGS = { h1: 'heading-one', h2: 'heading-two', h3: 'heading-three', + h4: 'heading-four, + h5: 'heading-five', + h6: 'heading-six', li: 'list-item', ol: 'numbered-list', pre: 'code-block', @@ -632,6 +635,9 @@ export default class MessageComposerInput extends React.Component { this.hasBlock('heading-one') || this.hasBlock('heading-two') || this.hasBlock('heading-three') || + this.hasBlock('heading-four') || + this.hasBlock('heading-five') || + this.hasBlock('heading-six') || this.hasBlock('code-block'))) { return change.setBlocks(DEFAULT_NODE); @@ -687,6 +693,9 @@ export default class MessageComposerInput extends React.Component { case 'heading-one': case 'heading-two': case 'heading-three': + case 'heading-four': + case 'heading-five': + case 'heading-six': case 'list-item': case 'code-block': { const isActive = this.hasBlock(type); @@ -1215,6 +1224,12 @@ export default class MessageComposerInput extends React.Component { return

    {children}

    ; case 'heading-three': return

    {children}

    ; + case 'heading-four': + return

    {children}

    ; + case 'heading-five': + return
    {children}
    ; + case 'heading-six': + return
    {children}
    ; case 'list-item': return
  • {children}
  • ; case 'numbered-list': From 65f0b0571902f723fefee82b90a62c47fc73a54c Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 23:40:22 +0100 Subject: [PATCH 041/123] fix typo --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 30461ca816..a426d69918 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -92,7 +92,7 @@ const BLOCK_TAGS = { h1: 'heading-one', h2: 'heading-two', h3: 'heading-three', - h4: 'heading-four, + h4: 'heading-four', h5: 'heading-five', h6: 'heading-six', li: 'list-item', From 117519566e2721fa50e422db26b4baeab603b3bd Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 19 May 2018 23:40:48 +0100 Subject: [PATCH 042/123] remove HRs from H1/H2s --- res/css/views/rooms/_EventTile.scss | 1 + 1 file changed, 1 insertion(+) diff --git a/res/css/views/rooms/_EventTile.scss b/res/css/views/rooms/_EventTile.scss index ce2bf9c8a4..67c8b8b2d8 100644 --- a/res/css/views/rooms/_EventTile.scss +++ b/res/css/views/rooms/_EventTile.scss @@ -443,6 +443,7 @@ limitations under the License. .mx_EventTile_content .markdown-body h2 { font-size: 1.5em; + border-bottom: none ! important; // override GFM } .mx_EventTile_content .markdown-body a { From f2116943c89c3b8ec4b58a270a5b22de7739ff98 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 00:17:11 +0100 Subject: [PATCH 043/123] switch schema to match the MD serializer --- .../views/rooms/MessageComposerInput.js | 80 +++++++++---------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index a426d69918..5126fb2813 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -84,17 +84,17 @@ const DEFAULT_NODE = 'paragraph'; // map HTML elements through to our Slate schema node types // used for the HTML deserializer. -// (We don't use the same names so that they are closer to the MD serializer's schema) +// (The names here are chosen to match the MD serializer's schema for convenience) const BLOCK_TAGS = { p: 'paragraph', blockquote: 'block-quote', ul: 'bulleted-list', - h1: 'heading-one', - h2: 'heading-two', - h3: 'heading-three', - h4: 'heading-four', - h5: 'heading-five', - h6: 'heading-six', + h1: 'heading1', + h2: 'heading2', + h3: 'heading3', + h4: 'heading4', + h5: 'heading5', + h6: 'heading6', li: 'list-item', ol: 'numbered-list', pre: 'code-block', @@ -106,10 +106,10 @@ const MARK_TAGS = { em: 'italic', i: 'italic', // deprecated code: 'code', - u: 'underline', - del: 'strikethrough', - strike: 'strikethrough', // deprecated - s: 'strikethrough', // deprecated + u: 'underlined', + del: 'deleted', + strike: 'deleted', // deprecated + s: 'deleted', // deprecated }; function onSendMessageFailed(err, room) { @@ -513,8 +513,8 @@ export default class MessageComposerInput extends React.Component { enableRichtext(enabled: boolean) { if (enabled === this.state.isRichtextEnabled) return; - // FIXME: this conversion should be handled in the store, surely - // i.e. "convert my current composer value into Rich or MD, as ComposerHistoryManager already does" + // FIXME: this duplicates similar conversions which happen in the history & store. + // they should be factored out. let editorState = null; if (enabled) { @@ -540,6 +540,7 @@ export default class MessageComposerInput extends React.Component { editorState: this.createEditorState(enabled, editorState), isRichtextEnabled: enabled, }); + SettingsStore.setValue("MessageComposerInput.isRichTextEnabled", null, SettingLevel.ACCOUNT, enabled); }; @@ -588,7 +589,7 @@ export default class MessageComposerInput extends React.Component { [KeyCode.KEY_M]: 'toggle-mode', [KeyCode.KEY_B]: 'bold', [KeyCode.KEY_I]: 'italic', - [KeyCode.KEY_U]: 'underline', + [KeyCode.KEY_U]: 'underlined', [KeyCode.KEY_J]: 'code', }[ev.keyCode]; @@ -632,12 +633,12 @@ export default class MessageComposerInput extends React.Component { } else if (editorState.anchorOffset == 0 && (this.hasBlock('block-quote') || - this.hasBlock('heading-one') || - this.hasBlock('heading-two') || - this.hasBlock('heading-three') || - this.hasBlock('heading-four') || - this.hasBlock('heading-five') || - this.hasBlock('heading-six') || + this.hasBlock('heading1') || + this.hasBlock('heading2') || + this.hasBlock('heading3') || + this.hasBlock('heading4') || + this.hasBlock('heading5') || + this.hasBlock('heading6') || this.hasBlock('code-block'))) { return change.setBlocks(DEFAULT_NODE); @@ -690,12 +691,12 @@ export default class MessageComposerInput extends React.Component { // simple blocks case 'paragraph': case 'block-quote': - case 'heading-one': - case 'heading-two': - case 'heading-three': - case 'heading-four': - case 'heading-five': - case 'heading-six': + case 'heading1': + case 'heading2': + case 'heading3': + case 'heading4': + case 'heading5': + case 'heading6': case 'list-item': case 'code-block': { const isActive = this.hasBlock(type); @@ -716,8 +717,8 @@ export default class MessageComposerInput extends React.Component { case 'bold': case 'italic': case 'code': - case 'underline': - case 'strikethrough': { + case 'underlined': + case 'deleted': { change.toggleMark(type); } break; @@ -830,10 +831,6 @@ export default class MessageComposerInput extends React.Component { handleReturn = (ev, change) => { if (ev.shiftKey) { - - // FIXME: we should insert a
    equivalent rather than letting Slate - // split the current block, otherwise

    will be split into two paragraphs - // and it'll look like a double line-break. return change.insertText('\n'); } @@ -1218,17 +1215,17 @@ export default class MessageComposerInput extends React.Component { return

    {children}
    ; case 'bulleted-list': return
      {children}
    ; - case 'heading-one': + case 'heading1': return

    {children}

    ; - case 'heading-two': + case 'heading2': return

    {children}

    ; - case 'heading-three': + case 'heading3': return

    {children}

    ; - case 'heading-four': + case 'heading4': return

    {children}

    ; - case 'heading-five': + case 'heading5': return
    {children}
    ; - case 'heading-six': + case 'heading6': return
    {children}
    ; case 'list-item': return
  • {children}
  • ; @@ -1289,9 +1286,9 @@ export default class MessageComposerInput extends React.Component { return {children}; case 'code': return {children}; - case 'underline': + case 'underlined': return {children}; - case 'strikethrough': + case 'deleted': return {children}; } }; @@ -1304,7 +1301,8 @@ export default class MessageComposerInput extends React.Component { quote: 'block-quote', bullet: 'bulleted-list', numbullet: 'numbered-list', - strike: 'strikethrough', + underline: 'underlined', + strike: 'deleted', }[name] || name; this.handleKeyCommand(command); }; From c3a6a41e5ded05be0bb370644cbd5e0079a821bf Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 00:49:29 +0100 Subject: [PATCH 044/123] support links in RTE --- src/autocomplete/PlainWithPillsSerializer.js | 2 +- .../views/rooms/MessageComposerInput.js | 31 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/autocomplete/PlainWithPillsSerializer.js b/src/autocomplete/PlainWithPillsSerializer.js index 0e850f2a33..8fa73be6a3 100644 --- a/src/autocomplete/PlainWithPillsSerializer.js +++ b/src/autocomplete/PlainWithPillsSerializer.js @@ -69,7 +69,7 @@ class PlainWithPillsSerializer { case 'plain': return node.data.get('completion'); case 'md': - return `[${ node.text }](${ node.data.get('url') })`; + return `[${ node.text }](${ node.data.get('href') })`; case 'id': return node.data.get('completionId') || node.data.get('completion'); } diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 5126fb2813..5853525832 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -200,6 +200,31 @@ export default class MessageComposerInput extends React.Component { nodes: next(el.childNodes), } } + // special case links + if (tag === 'a') { + const href = el.getAttribute('href'); + let m = href.match(MATRIXTO_URL_PATTERN); + if (m) { + return { + object: 'inline', + type: 'pill', + data: { + href, + completion: el.innerText, + completionId: m[1], + }, + isVoid: true, + } + } + else { + return { + object: 'inline', + type: 'link', + data: { href }, + nodes: next(el.childNodes), + } + } + } }, serialize: (obj, children) => { if (obj.object === 'block' || obj.object === 'inline') { @@ -1161,7 +1186,7 @@ export default class MessageComposerInput extends React.Component { if (href) { inline = Inline.create({ type: 'pill', - data: { completion, completionId, url: href }, + data: { completion, completionId, href }, // we can't put text in here otherwise the editor tries to select it isVoid: true, }); @@ -1233,9 +1258,11 @@ export default class MessageComposerInput extends React.Component { return
      {children}
    ; case 'code-block': return
    {children}
    ; + case 'link': + return {children}; case 'pill': { const { data } = node; - const url = data.get('url'); + const url = data.get('href'); const completion = data.get('completion'); const shouldShowPillAvatar = !SettingsStore.getValue("Pill.shouldHidePillAvatar"); From d76a2aba9baa055614effe28501ed83800e07804 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 01:07:25 +0100 Subject: [PATCH 045/123] use

    as our root node everywhere and fix blank roundtrip bug --- .../views/rooms/MessageComposerInput.js | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 5853525832..754f208373 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -78,8 +78,7 @@ const ENTITY_TYPES = { AT_ROOM_PILL: 'ATROOMPILL', }; -// the Slate node type to default to for unstyled text when in RTE mode. -// (we use 'line' for oneliners however) +// the Slate node type to default to for unstyled text const DEFAULT_NODE = 'paragraph'; // map HTML elements through to our Slate schema node types @@ -259,7 +258,7 @@ export default class MessageComposerInput extends React.Component { } else { // ...or create a new one. - return Plain.deserialize('') + return Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); } } @@ -544,7 +543,13 @@ export default class MessageComposerInput extends React.Component { let editorState = null; if (enabled) { // for simplicity when roundtripping, we use slate-md-serializer rather than commonmark - editorState = this.md.deserialize(this.plainWithMdPills.serialize(this.state.editorState)); + const markdown = this.plainWithMdPills.serialize(this.state.editorState); + if (markdown !== '') { + editorState = this.md.deserialize(markdown); + } + else { + editorState = Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); + } // the alternative would be something like: // @@ -556,7 +561,10 @@ export default class MessageComposerInput extends React.Component { // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); // value = ContentState.createFromText(markdown); - editorState = Plain.deserialize(this.md.serialize(this.state.editorState)); + editorState = Plain.deserialize( + this.md.serialize(this.state.editorState), + { defaultBlock: DEFAULT_NODE } + ); } Analytics.setRichtextMode(enabled); @@ -937,6 +945,7 @@ export default class MessageComposerInput extends React.Component { if (contentText === '') return true; if (shouldSendHTML) { + // FIXME: should we strip out the surrounding

    ? contentHTML = this.html.serialize(editorState); // HtmlUtils.processHtmlForSending(); } } else { @@ -1230,10 +1239,6 @@ export default class MessageComposerInput extends React.Component { const { attributes, children, node, isSelected } = props; switch (node.type) { - case 'line': - // ideally we'd return { children }
    , but as this isn't - // a valid react component, we don't have much choice. - return
    {children}
    ; case 'paragraph': return

    {children}

    ; case 'block-quote': From a0d88a829da8ee71b3a7910e1c534f808727f36a Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 02:53:32 +0100 Subject: [PATCH 046/123] support sending inlines from the RTE. includes a horrific hack for sending emoji until https://github.com/ianstormtaylor/slate/pull/1854 is merged or otherwise solved --- .../views/rooms/MessageComposerInput.js | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 754f208373..8c5ab2394f 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -202,12 +202,15 @@ export default class MessageComposerInput extends React.Component { // special case links if (tag === 'a') { const href = el.getAttribute('href'); - let m = href.match(MATRIXTO_URL_PATTERN); + let m; + if (href) { + m = href.match(MATRIXTO_URL_PATTERN); + } if (m) { return { object: 'inline', type: 'pill', - data: { + data: { href, completion: el.innerText, completionId: m[1], @@ -226,7 +229,7 @@ export default class MessageComposerInput extends React.Component { } }, serialize: (obj, children) => { - if (obj.object === 'block' || obj.object === 'inline') { + if (obj.object === 'block') { return this.renderNode({ node: obj, children: children, @@ -238,6 +241,26 @@ export default class MessageComposerInput extends React.Component { children: children, }); } + else if (obj.object === 'inline') { + // special case links, pills and emoji otherwise we + // end up with React components getting rendered out(!) + switch (obj.type) { + case 'pill': + return { obj.data.get('completion') }; + case 'link': + return { children }; + case 'emoji': + // XXX: apparently you can't return plain strings from serializer rules + // until https://github.com/ianstormtaylor/slate/pull/1854 is merged. + // So instead we temporarily wrap emoji from RTE in an arbitrary tag + // (). would be nicer, but in practice it causes CSS issues. + return { obj.data.get('emojiUnicode') }; + } + return this.renderNode({ + node: obj, + children: children, + }); + } } } ] @@ -545,6 +568,7 @@ export default class MessageComposerInput extends React.Component { // for simplicity when roundtripping, we use slate-md-serializer rather than commonmark const markdown = this.plainWithMdPills.serialize(this.state.editorState); if (markdown !== '') { + // weirdly, the Md serializer can't deserialize '' to a valid Value... editorState = this.md.deserialize(markdown); } else { @@ -572,6 +596,8 @@ export default class MessageComposerInput extends React.Component { this.setState({ editorState: this.createEditorState(enabled, editorState), isRichtextEnabled: enabled, + }, ()=>{ + this.refs.editor.focus(); }); SettingsStore.setValue("MessageComposerInput.isRichTextEnabled", null, SettingLevel.ACCOUNT, enabled); @@ -852,6 +878,8 @@ export default class MessageComposerInput extends React.Component { return this.props.onFilesPasted(transfer.files); } else if (transfer.type === "html") { + // FIXME: https://github.com/ianstormtaylor/slate/issues/1497 means + // that we will silently discard nested blocks (e.g. nested lists) :( const fragment = this.html.deserialize(transfer.html); if (this.state.isRichtextEnabled) { return change.insertFragment(fragment.document); @@ -1263,7 +1291,7 @@ export default class MessageComposerInput extends React.Component { return
      {children}
    ; case 'code-block': return
    {children}
    ; - case 'link': + case 'link': return {children}; case 'pill': { const { data } = node; From e9cabf0e8564a30f7e0432fac063144e4a28a8be Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 03:17:51 +0100 Subject: [PATCH 047/123] add pill and emoji serialisation to Md --- .../views/rooms/MessageComposerInput.js | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 8c5ab2394f..d1bf4e4544 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -177,8 +177,23 @@ export default class MessageComposerInput extends React.Component { this.plainWithMdPills = new PlainWithPillsSerializer({ pillFormat: 'md' }); this.plainWithIdPills = new PlainWithPillsSerializer({ pillFormat: 'id' }); this.plainWithPlainPills = new PlainWithPillsSerializer({ pillFormat: 'plain' }); - this.md = new Md(); - this.html = new Html({ + + this.md = new Md({ + rules: [ + { + serialize: (obj, children) => { + switch (obj.type) { + case 'pill': + return `[${ obj.data.get('completion') }](${ obj.data.get('href') })`; + case 'emoji': + return obj.data.get('emojiUnicode'); + } + } + } + ] + }); + + this.html = new Html({ rules: [ { deserialize: (el, next) => { @@ -567,8 +582,13 @@ export default class MessageComposerInput extends React.Component { if (enabled) { // for simplicity when roundtripping, we use slate-md-serializer rather than commonmark const markdown = this.plainWithMdPills.serialize(this.state.editorState); + + // weirdly, the Md serializer can't deserialize '' to a valid Value... if (markdown !== '') { - // weirdly, the Md serializer can't deserialize '' to a valid Value... + // FIXME: the MD deserializer doesn't know how to deserialize pills + // and gives no hooks for doing so, so we should manually fix up + // the editorState first in order to preserve them. + editorState = this.md.deserialize(markdown); } else { From ad7782bc22628f633f147f3df3066a0d106269a0 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 14:07:33 +0100 Subject: [PATCH 048/123] remove the remaining Draft specific stuff from RichText --- src/RichText.js | 150 ------------------------------------------------ 1 file changed, 150 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index e3162a4e2c..65b5dad107 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -18,48 +18,11 @@ limitations under the License. import React from 'react'; -/* -import { - Editor, - EditorState, - Modifier, - ContentState, - ContentBlock, - convertFromHTML, - DefaultDraftBlockRenderMap, - DefaultDraftInlineStyle, - CompositeDecorator, - SelectionState, - Entity, -} from 'draft-js'; -import { stateToMarkdown as __stateToMarkdown } from 'draft-js-export-markdown'; -*/ - -import Html from 'slate-html-serializer'; - import * as sdk from './index'; import * as emojione from 'emojione'; import { SelectionRange } from "./autocomplete/Autocompleter"; -const MARKDOWN_REGEX = { - LINK: /(?:\[([^\]]+)\]\(([^\)]+)\))|\<(\w+:\/\/[^\>]+)\>/g, - ITALIC: /([\*_])([\w\s]+?)\1/g, - BOLD: /([\*_])\1([\w\s]+?)\1\1/g, - HR: /(\n|^)((-|\*|_) *){3,}(\n|$)/g, - CODE: /`[^`]*`/g, - STRIKETHROUGH: /~{2}[^~]*~{2}/g, -}; - -const ZWS_CODE = 8203; -const ZWS = String.fromCharCode(ZWS_CODE); // zero width space - -export function stateToMarkdown(state) { - return __stateToMarkdown(state) - .replace( - ZWS, // draft-js-export-markdown adds these - ''); // this is *not* a zero width space, trust me :) -} export function unicodeToEmojiUri(str) { let replaceWith, unicode, alt; @@ -87,116 +50,3 @@ export function unicodeToEmojiUri(str) { return str; } - -/** - * Utility function that looks for regex matches within a ContentBlock and invokes {callback} with (start, end) - * From https://facebook.github.io/draft-js/docs/advanced-topics-decorators.html - */ -function findWithRegex(regex, contentBlock: ContentBlock, callback: (start: number, end: number) => any) { - const text = contentBlock.getText(); - let matchArr, start; - while ((matchArr = regex.exec(text)) !== null) { - start = matchArr.index; - callback(start, start + matchArr[0].length); - } -} - -/** - * Returns a composite decorator which has access to provided scope. - */ -export function getScopedRTDecorators(scope: any): CompositeDecorator { - return [emojiDecorator]; -} - -export function getScopedMDDecorators(scope: any): CompositeDecorator { - const markdownDecorators = ['HR', 'BOLD', 'ITALIC', 'CODE', 'STRIKETHROUGH'].map( - (style) => ({ - strategy: (contentState, contentBlock, callback) => { - return findWithRegex(MARKDOWN_REGEX[style], contentBlock, callback); - }, - component: (props) => ( - - { props.children } - - ), - })); - - markdownDecorators.push({ - strategy: (contentState, contentBlock, callback) => { - return findWithRegex(MARKDOWN_REGEX.LINK, contentBlock, callback); - }, - component: (props) => ( - - { props.children } - - ), - }); - // markdownDecorators.push(emojiDecorator); - // TODO Consider renabling "syntax highlighting" when we can do it properly - return [emojiDecorator]; -} - -/** - * Passes rangeToReplace to modifyFn and replaces it in contentState with the result. - */ -export function modifyText(contentState: ContentState, rangeToReplace: SelectionState, - modifyFn: (text: string) => string, inlineStyle, entityKey): ContentState { - let getText = (key) => contentState.getBlockForKey(key).getText(), - startKey = rangeToReplace.getStartKey(), - startOffset = rangeToReplace.getStartOffset(), - endKey = rangeToReplace.getEndKey(), - endOffset = rangeToReplace.getEndOffset(), - text = ""; - - - for (let currentKey = startKey; - currentKey && currentKey !== endKey; - currentKey = contentState.getKeyAfter(currentKey)) { - const blockText = getText(currentKey); - text += blockText.substring(startOffset, blockText.length); - - // from now on, we'll take whole blocks - startOffset = 0; - } - - // add remaining part of last block - text += getText(endKey).substring(startOffset, endOffset); - - return Modifier.replaceText(contentState, rangeToReplace, modifyFn(text), inlineStyle, entityKey); -} - -/** - * Computes the plaintext offsets of the given SelectionState. - * Note that this inherently means we make assumptions about what that means (no separator between ContentBlocks, etc) - * Used by autocomplete to show completions when the current selection lies within, or at the edges of a command. - */ -export function selectionStateToTextOffsets(selectionState: SelectionState, - contentBlocks: Array): {start: number, end: number} { - let offset = 0, start = 0, end = 0; - for (const block of contentBlocks) { - if (selectionState.getStartKey() === block.getKey()) { - start = offset + selectionState.getStartOffset(); - } - if (selectionState.getEndKey() === block.getKey()) { - end = offset + selectionState.getEndOffset(); - break; - } - offset += block.getLength(); - } - - return { - start, - end, - }; -} - -export function hasMultiLineSelection(editorState: EditorState): boolean { - const selectionState = editorState.getSelection(); - const anchorKey = selectionState.getAnchorKey(); - const currentContent = editorState.getCurrentContent(); - const currentContentBlock = currentContent.getBlockForKey(anchorKey); - const start = selectionState.getStartOffset(); - const end = selectionState.getEndOffset(); - const selectedText = currentContentBlock.getText().slice(start, end); - return selectedText.includes('\n'); -} From c5676eef89aa9784a78040ddd6ed16e117aa7d80 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 14:32:06 +0100 Subject: [PATCH 049/123] comment out more old draft stuff --- src/HtmlUtils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 7ca404be31..4c1564297d 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -112,7 +112,7 @@ export function charactersToImageNode(alt, useSvg, ...unicode) { />; } - +/* export function processHtmlForSending(html: string): string { const contentDiv = document.createElement('div'); contentDiv.innerHTML = html; @@ -146,6 +146,7 @@ export function processHtmlForSending(html: string): string { return contentHTML; } +*/ /* * Given an untrusted HTML string, return a React node with an sanitized version From 9aba046f21d67b19b5e3b5c4a13814e919f56446 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 14:32:20 +0100 Subject: [PATCH 050/123] fix MD pill serialization --- src/autocomplete/PlainWithPillsSerializer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autocomplete/PlainWithPillsSerializer.js b/src/autocomplete/PlainWithPillsSerializer.js index 8fa73be6a3..7428241b05 100644 --- a/src/autocomplete/PlainWithPillsSerializer.js +++ b/src/autocomplete/PlainWithPillsSerializer.js @@ -69,7 +69,7 @@ class PlainWithPillsSerializer { case 'plain': return node.data.get('completion'); case 'md': - return `[${ node.text }](${ node.data.get('href') })`; + return `[${ node.data.get('completion') }](${ node.data.get('href') })`; case 'id': return node.data.get('completionId') || node.data.get('completion'); } From aac6866779f935a23e876a0ffc5efc5e881c7168 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 14:33:14 +0100 Subject: [PATCH 051/123] switch back to using commonmark for serialising MD when roundtripping and escape MD correctly when serialising via slate-md-serializer --- .../views/rooms/MessageComposerInput.js | 60 +++++++++++-------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index d1bf4e4544..0d603d3135 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -182,11 +182,21 @@ export default class MessageComposerInput extends React.Component { rules: [ { serialize: (obj, children) => { - switch (obj.type) { - case 'pill': - return `[${ obj.data.get('completion') }](${ obj.data.get('href') })`; - case 'emoji': - return obj.data.get('emojiUnicode'); + if (obj.object === 'string') { + // escape any MD in it. i have no idea why the serializer doesn't + // do this already. + // TODO: this can probably be more robust - it doesn't consider + // indenting or lists for instance. + return children.replace(/([*_~`+])/g, '\\$1') + .replace(/^([>#\|])/g, '\\$1'); + } + else if (obj.object === 'inline') { + switch (obj.type) { + case 'pill': + return `[${ obj.data.get('completion') }](${ obj.data.get('href') })`; + case 'emoji': + return obj.data.get('emojiUnicode'); + } } } } @@ -580,27 +590,29 @@ export default class MessageComposerInput extends React.Component { let editorState = null; if (enabled) { - // for simplicity when roundtripping, we use slate-md-serializer rather than commonmark - const markdown = this.plainWithMdPills.serialize(this.state.editorState); - - // weirdly, the Md serializer can't deserialize '' to a valid Value... - if (markdown !== '') { - // FIXME: the MD deserializer doesn't know how to deserialize pills - // and gives no hooks for doing so, so we should manually fix up - // the editorState first in order to preserve them. - - editorState = this.md.deserialize(markdown); - } - else { - editorState = Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); - } - - // the alternative would be something like: + // for consistency when roundtripping, we could use slate-md-serializer rather than + // commonmark, but then we would lose pills as the MD deserialiser doesn't know about + // them and doesn't have any extensibility hooks. // - // const sourceWithPills = this.plainWithMdPills.serialize(this.state.editorState); - // const markdown = new Markdown(sourceWithPills); - // editorState = this.html.deserialize(markdown.toHTML()); + // The code looks like this: + // + // const markdown = this.plainWithMdPills.serialize(this.state.editorState); + // + // // weirdly, the Md serializer can't deserialize '' to a valid Value... + // if (markdown !== '') { + // editorState = this.md.deserialize(markdown); + // } + // else { + // editorState = Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); + // } + // so, instead, we use commonmark proper (which is arguably more logical to the user + // anyway, as they'll expect the RTE view to match what they'll see in the timeline, + // but the HTML->MD conversion is anyone's guess). + + const sourceWithPills = this.plainWithMdPills.serialize(this.state.editorState); + const markdown = new Markdown(sourceWithPills); + editorState = this.html.deserialize(markdown.toHTML()); } else { // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); // value = ContentState.createFromText(markdown); From d799b7e424d54a52bc91d83ab17e06dead767964 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 16:30:39 +0100 Subject: [PATCH 052/123] refactor roundtripping into a single place and fix isRichTextEnabled to be correctly camelCased everywhere... --- src/ComposerHistoryManager.js | 34 +---- src/components/views/rooms/MessageComposer.js | 8 +- .../views/rooms/MessageComposerInput.js | 129 ++++++++++-------- .../views/rooms/MessageComposerInput-test.js | 2 +- 4 files changed, 84 insertions(+), 89 deletions(-) diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index 28749ace15..f997e1d1cd 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -28,8 +28,8 @@ type MessageFormat = 'rich' | 'markdown'; class HistoryItem { - // Keeping message for backwards-compatibility - message: string; + // We store history items in their native format to ensure history is accurate + // and then convert them if our RTE has subsequently changed format. value: Value; format: MessageFormat = 'rich'; @@ -51,32 +51,6 @@ class HistoryItem { format: this.format }; } - - // FIXME: rather than supporting storing history in either format, why don't we pick - // one canonical form? - toValue(outputFormat: MessageFormat): Value { - if (outputFormat === 'markdown') { - if (this.format === 'rich') { - // convert a rich formatted history entry to its MD equivalent - return Plain.deserialize(Md.serialize(this.value)); - // return ContentState.createFromText(RichText.stateToMarkdown(contentState)); - } - else if (this.format === 'markdown') { - return this.value; - } - } else if (outputFormat === 'rich') { - if (this.format === 'markdown') { - // convert MD formatted string to its rich equivalent. - return Md.deserialize(Plain.serialize(this.value)); - // return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML()); - } - else if (this.format === 'rich') { - return this.value; - } - } - console.error("unknown format -> outputFormat conversion"); - return this.value; - } } export default class ComposerHistoryManager { @@ -110,9 +84,9 @@ export default class ComposerHistoryManager { sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item.toJSON())); } - getItem(offset: number, format: MessageFormat): ?Value { + getItem(offset: number): ?HistoryItem { this.currentIndex = _clamp(this.currentIndex + offset, 0, this.lastIndex - 1); const item = this.history[this.currentIndex]; - return item ? item.toValue(format) : null; + return item; } } diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 9aaa33f0fa..157dc9e704 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -46,7 +46,7 @@ export default class MessageComposer extends React.Component { inputState: { marks: [], blockType: null, - isRichtextEnabled: SettingsStore.getValue('MessageComposerInput.isRichTextEnabled'), + isRichTextEnabled: SettingsStore.getValue('MessageComposerInput.isRichTextEnabled'), }, showFormatting: SettingsStore.getValue('MessageComposer.showFormatting'), isQuoting: Boolean(RoomViewStore.getQuotingEvent()), @@ -227,7 +227,7 @@ export default class MessageComposer extends React.Component { onToggleMarkdownClicked(e) { e.preventDefault(); // don't steal focus from the editor! - this.messageComposerInput.enableRichtext(!this.state.inputState.isRichtextEnabled); + this.messageComposerInput.enableRichtext(!this.state.inputState.isRichTextEnabled); } render() { @@ -380,10 +380,10 @@ export default class MessageComposer extends React.Component {
    { formatButtons }
    - + src={`img/button-md-${!this.state.inputState.isRichTextEnabled}.png`} /> ${body}`); - if (!this.state.isRichtextEnabled) { + if (!this.state.isRichTextEnabled) { content = ContentState.createFromText(RichText.stateToMarkdown(content)); } @@ -374,7 +374,7 @@ export default class MessageComposerInput extends React.Component { startSelection, blockMap); startSelection = SelectionState.createEmpty(contentState.getFirstBlock().getKey()); - if (this.state.isRichtextEnabled) { + if (this.state.isRichTextEnabled) { contentState = Modifier.setBlockType(contentState, startSelection, 'blockquote'); } let editorState = EditorState.push(this.state.editorState, contentState, 'insert-characters'); @@ -582,52 +582,61 @@ export default class MessageComposerInput extends React.Component { }); }; - enableRichtext(enabled: boolean) { - if (enabled === this.state.isRichtextEnabled) return; + mdToRichEditorState(editorState: Value): Value { + // for consistency when roundtripping, we could use slate-md-serializer rather than + // commonmark, but then we would lose pills as the MD deserialiser doesn't know about + // them and doesn't have any extensibility hooks. + // + // The code looks like this: + // + // const markdown = this.plainWithMdPills.serialize(editorState); + // + // // weirdly, the Md serializer can't deserialize '' to a valid Value... + // if (markdown !== '') { + // editorState = this.md.deserialize(markdown); + // } + // else { + // editorState = Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); + // } - // FIXME: this duplicates similar conversions which happen in the history & store. - // they should be factored out. + // so, instead, we use commonmark proper (which is arguably more logical to the user + // anyway, as they'll expect the RTE view to match what they'll see in the timeline, + // but the HTML->MD conversion is anyone's guess). + + const textWithMdPills = this.plainWithMdPills.serialize(editorState); + const markdown = new Markdown(textWithMdPills); + // HTML deserialize has custom rules to turn matrix.to links into pill objects. + return this.html.deserialize(markdown.toHTML()); + } + + richToMdEditorState(editorState: Value): Value { + // FIXME: this conversion loses pills (turning them into pure MD links). + // We need to add a pill-aware deserialize method + // to PlainWithPillsSerializer which recognises pills in raw MD and turns them into pills. + return Plain.deserialize( + // FIXME: we compile the MD out of the RTE state using slate-md-serializer + // which doesn't roundtrip symmetrically with commonmark, which we use for + // compiling MD out of the MD editor state above. + this.md.serialize(editorState), + { defaultBlock: DEFAULT_NODE } + ); + } + + enableRichtext(enabled: boolean) { + if (enabled === this.state.isRichTextEnabled) return; let editorState = null; if (enabled) { - // for consistency when roundtripping, we could use slate-md-serializer rather than - // commonmark, but then we would lose pills as the MD deserialiser doesn't know about - // them and doesn't have any extensibility hooks. - // - // The code looks like this: - // - // const markdown = this.plainWithMdPills.serialize(this.state.editorState); - // - // // weirdly, the Md serializer can't deserialize '' to a valid Value... - // if (markdown !== '') { - // editorState = this.md.deserialize(markdown); - // } - // else { - // editorState = Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); - // } - - // so, instead, we use commonmark proper (which is arguably more logical to the user - // anyway, as they'll expect the RTE view to match what they'll see in the timeline, - // but the HTML->MD conversion is anyone's guess). - - const sourceWithPills = this.plainWithMdPills.serialize(this.state.editorState); - const markdown = new Markdown(sourceWithPills); - editorState = this.html.deserialize(markdown.toHTML()); + editorState = this.mdToRichEditorState(this.state.editorState); } else { - // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); - // value = ContentState.createFromText(markdown); - - editorState = Plain.deserialize( - this.md.serialize(this.state.editorState), - { defaultBlock: DEFAULT_NODE } - ); + editorState = this.richToMdEditorState(this.state.editorState); } Analytics.setRichtextMode(enabled); this.setState({ editorState: this.createEditorState(enabled, editorState), - isRichtextEnabled: enabled, + isRichTextEnabled: enabled, }, ()=>{ this.refs.editor.focus(); }); @@ -710,7 +719,7 @@ export default class MessageComposerInput extends React.Component { }; onBackspace = (ev: Event, change: Change): Change => { - if (this.state.isRichtextEnabled) { + if (this.state.isRichTextEnabled) { // let backspace exit lists const isList = this.hasBlock('list-item'); const { editorState } = this.state; @@ -740,14 +749,14 @@ export default class MessageComposerInput extends React.Component { handleKeyCommand = (command: string): boolean => { if (command === 'toggle-mode') { - this.enableRichtext(!this.state.isRichtextEnabled); + this.enableRichtext(!this.state.isRichTextEnabled); return true; } let newState: ?Value = null; // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown. - if (this.state.isRichtextEnabled) { + if (this.state.isRichTextEnabled) { const type = command; const { editorState } = this.state; const change = editorState.change(); @@ -913,7 +922,7 @@ export default class MessageComposerInput extends React.Component { // FIXME: https://github.com/ianstormtaylor/slate/issues/1497 means // that we will silently discard nested blocks (e.g. nested lists) :( const fragment = this.html.deserialize(transfer.html); - if (this.state.isRichtextEnabled) { + if (this.state.isRichTextEnabled) { return change.insertFragment(fragment.document); } else { @@ -954,7 +963,7 @@ export default class MessageComposerInput extends React.Component { if (cmd) { if (!cmd.error) { - this.historyManager.save(editorState, this.state.isRichtextEnabled ? 'rich' : 'markdown'); + this.historyManager.save(editorState, this.state.isRichTextEnabled ? 'rich' : 'markdown'); this.setState({ editorState: this.createEditorState(), }); @@ -986,7 +995,7 @@ export default class MessageComposerInput extends React.Component { const replyingToEv = RoomViewStore.getQuotingEvent(); const mustSendHTML = Boolean(replyingToEv); - if (this.state.isRichtextEnabled) { + if (this.state.isRichTextEnabled) { // We should only send HTML if any block is styled or contains inline style let shouldSendHTML = false; @@ -1032,7 +1041,7 @@ export default class MessageComposerInput extends React.Component { this.historyManager.save( editorState, - this.state.isRichtextEnabled ? 'rich' : 'markdown', + this.state.isRichTextEnabled ? 'rich' : 'markdown', ); if (commandText && commandText.startsWith('/me')) { @@ -1119,7 +1128,7 @@ export default class MessageComposerInput extends React.Component { if (up) { const scrollCorrection = editorNode.scrollTop; const distanceFromTop = cursorRect.top - editorRect.top + scrollCorrection; - console.log(`Cursor distance from editor top is ${distanceFromTop}`); + //console.log(`Cursor distance from editor top is ${distanceFromTop}`); if (distanceFromTop < EDGE_THRESHOLD) { navigateHistory = true; } @@ -1128,7 +1137,7 @@ export default class MessageComposerInput extends React.Component { const scrollCorrection = editorNode.scrollHeight - editorNode.clientHeight - editorNode.scrollTop; const distanceFromBottom = editorRect.bottom - cursorRect.bottom + scrollCorrection; - console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); + //console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); if (distanceFromBottom < EDGE_THRESHOLD) { navigateHistory = true; } @@ -1168,7 +1177,19 @@ export default class MessageComposerInput extends React.Component { return; } - let editorState = this.historyManager.getItem(delta, this.state.isRichtextEnabled ? 'rich' : 'markdown'); + let editorState; + const historyItem = this.historyManager.getItem(delta); + if (historyItem) { + if (historyItem.format === 'rich' && !this.state.isRichTextEnabled) { + editorState = this.richToMdEditorState(historyItem.value); + } + else if (historyItem.format === 'markdown' && this.state.isRichTextEnabled) { + editorState = this.mdToRichEditorState(historyItem.value); + } + else { + editorState = historyItem.value; + } + } // Move selection to the end of the selected history const change = editorState.change().collapseToEndOf(editorState.document); @@ -1468,8 +1489,8 @@ export default class MessageComposerInput extends React.Component {
    + title={this.state.isRichTextEnabled ? _t("Markdown is disabled") : _t("Markdown is enabled")} + src={`img/button-md-${!this.state.isRichTextEnabled}.png`} /> { 'mx_MessageComposer_input_markdownIndicator'); ReactTestUtils.Simulate.click(indicator); - expect(mci.state.isRichtextEnabled).toEqual(false, 'should have changed mode'); + expect(mci.state.isRichTextEnabled).toEqual(false, 'should have changed mode'); done(); }); }); From f981d7b7293c0eb2ad869091cabbb836b0c86a23 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 22:39:40 +0100 Subject: [PATCH 053/123] unify buttons on the node type names, and make them work --- ...o-n.svg => button-text-block-quote-on.svg} | 0 ...-quote.svg => button-text-block-quote.svg} | 0 ...t-bold-o-n.svg => button-text-bold-on.svg} | 0 ...n.svg => button-text-bulleted-list-on.svg} | 0 ...llet.svg => button-text-bulleted-list.svg} | 0 ...t-code-o-n.svg => button-text-code-on.svg} | 0 ...ike-o-n.svg => button-text-deleted-on.svg} | 0 ...ext-strike.svg => button-text-deleted.svg} | 0 ...alic-o-n.svg => button-text-italic-on.svg} | 0 ...n.svg => button-text-numbered-list-on.svg} | 0 ...llet.svg => button-text-numbered-list.svg} | 0 ...-o-n.svg => button-text-underlined-on.svg} | 0 ...derline.svg => button-text-underlined.svg} | 0 src/components/views/rooms/MessageComposer.js | 58 ++++++++------- .../views/rooms/MessageComposerInput.js | 70 ++++++++++--------- 15 files changed, 68 insertions(+), 60 deletions(-) rename res/img/{button-text-quote-o-n.svg => button-text-block-quote-on.svg} (100%) rename res/img/{button-text-quote.svg => button-text-block-quote.svg} (100%) rename res/img/{button-text-bold-o-n.svg => button-text-bold-on.svg} (100%) rename res/img/{button-text-bullet-o-n.svg => button-text-bulleted-list-on.svg} (100%) rename res/img/{button-text-bullet.svg => button-text-bulleted-list.svg} (100%) rename res/img/{button-text-code-o-n.svg => button-text-code-on.svg} (100%) rename res/img/{button-text-strike-o-n.svg => button-text-deleted-on.svg} (100%) rename res/img/{button-text-strike.svg => button-text-deleted.svg} (100%) rename res/img/{button-text-italic-o-n.svg => button-text-italic-on.svg} (100%) rename res/img/{button-text-numbullet-o-n.svg => button-text-numbered-list-on.svg} (100%) rename res/img/{button-text-numbullet.svg => button-text-numbered-list.svg} (100%) rename res/img/{button-text-underline-o-n.svg => button-text-underlined-on.svg} (100%) rename res/img/{button-text-underline.svg => button-text-underlined.svg} (100%) diff --git a/res/img/button-text-quote-o-n.svg b/res/img/button-text-block-quote-on.svg similarity index 100% rename from res/img/button-text-quote-o-n.svg rename to res/img/button-text-block-quote-on.svg diff --git a/res/img/button-text-quote.svg b/res/img/button-text-block-quote.svg similarity index 100% rename from res/img/button-text-quote.svg rename to res/img/button-text-block-quote.svg diff --git a/res/img/button-text-bold-o-n.svg b/res/img/button-text-bold-on.svg similarity index 100% rename from res/img/button-text-bold-o-n.svg rename to res/img/button-text-bold-on.svg diff --git a/res/img/button-text-bullet-o-n.svg b/res/img/button-text-bulleted-list-on.svg similarity index 100% rename from res/img/button-text-bullet-o-n.svg rename to res/img/button-text-bulleted-list-on.svg diff --git a/res/img/button-text-bullet.svg b/res/img/button-text-bulleted-list.svg similarity index 100% rename from res/img/button-text-bullet.svg rename to res/img/button-text-bulleted-list.svg diff --git a/res/img/button-text-code-o-n.svg b/res/img/button-text-code-on.svg similarity index 100% rename from res/img/button-text-code-o-n.svg rename to res/img/button-text-code-on.svg diff --git a/res/img/button-text-strike-o-n.svg b/res/img/button-text-deleted-on.svg similarity index 100% rename from res/img/button-text-strike-o-n.svg rename to res/img/button-text-deleted-on.svg diff --git a/res/img/button-text-strike.svg b/res/img/button-text-deleted.svg similarity index 100% rename from res/img/button-text-strike.svg rename to res/img/button-text-deleted.svg diff --git a/res/img/button-text-italic-o-n.svg b/res/img/button-text-italic-on.svg similarity index 100% rename from res/img/button-text-italic-o-n.svg rename to res/img/button-text-italic-on.svg diff --git a/res/img/button-text-numbullet-o-n.svg b/res/img/button-text-numbered-list-on.svg similarity index 100% rename from res/img/button-text-numbullet-o-n.svg rename to res/img/button-text-numbered-list-on.svg diff --git a/res/img/button-text-numbullet.svg b/res/img/button-text-numbered-list.svg similarity index 100% rename from res/img/button-text-numbullet.svg rename to res/img/button-text-numbered-list.svg diff --git a/res/img/button-text-underline-o-n.svg b/res/img/button-text-underlined-on.svg similarity index 100% rename from res/img/button-text-underline-o-n.svg rename to res/img/button-text-underlined-on.svg diff --git a/res/img/button-text-underline.svg b/res/img/button-text-underlined.svg similarity index 100% rename from res/img/button-text-underline.svg rename to res/img/button-text-underlined.svg diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 157dc9e704..4d00927767 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -215,7 +215,7 @@ export default class MessageComposer extends React.Component { } } - onFormatButtonClicked(name: "bold" | "italic" | "strike" | "code" | "underline" | "quote" | "bullet" | "numbullet", event) { + onFormatButtonClicked(name, event) { event.preventDefault(); this.messageComposerInput.onFormatButtonClicked(name, event); } @@ -303,14 +303,14 @@ export default class MessageComposer extends React.Component {
    ); - const formattingButton = ( + const formattingButton = this.state.inputState.isRichTextEnabled ? ( - ); + ) : null; let placeholderText; if (this.state.isQuoting) { @@ -353,31 +353,27 @@ export default class MessageComposer extends React.Component { ); } - const {marks, blockType} = this.state.inputState; - const formatButtons = ["bold", "italic", "strike", "underline", "code", "quote", "bullet", "numbullet"].map( - (name) => { - const active = marks.includes(name) || blockType === name; - const suffix = active ? '-o-n' : ''; - const onFormatButtonClicked = this.onFormatButtonClicked.bind(this, name); - const className = 'mx_MessageComposer_format_button mx_filterFlipColor'; - return ; - }, - ); + let formatBar; + if (this.state.showFormatting) { + const {marks, blockType} = this.state.inputState; + const formatButtons = ["bold", "italic", "deleted", "underlined", "code", "block-quote", "bulleted-list", "numbered-list"].map( + (name) => { + const active = marks.some(mark => mark.type === name) || blockType === name; + const suffix = active ? '-on' : ''; + const onFormatButtonClicked = this.onFormatButtonClicked.bind(this, name); + const className = 'mx_MessageComposer_format_button mx_filterFlipColor'; + return ; + }, + ); - return ( -
    -
    -
    - { controls } -
    -
    + formatBar =
    -
    +
    { formatButtons }
    + } + + return ( +
    +
    +
    + { controls } +
    +
    + { formatBar }
    ); } diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index aac7c7ddbb..2d5e6d050d 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -132,9 +132,6 @@ export default class MessageComposerInput extends React.Component { // js-sdk Room object room: PropTypes.object.isRequired, - // called with current plaintext content (as a string) whenever it changes - onContentChanged: PropTypes.func, - onFilesPasted: PropTypes.func, onInputStateChanged: PropTypes.func, @@ -319,15 +316,6 @@ export default class MessageComposerInput extends React.Component { dis.unregister(this.dispatcherRef); } - componentWillUpdate(nextProps, nextState) { - // this is dirty, but moving all this state to MessageComposer is dirtier - if (this.props.onInputStateChanged && nextState !== this.state) { - const state = this.getSelectionInfo(nextState.editorState); - state.isRichTextEnabled = nextState.isRichTextEnabled; - this.props.onInputStateChanged(state); - } - } - onAction = (payload) => { const editor = this.refs.editor; let editorState = this.state.editorState; @@ -567,6 +555,27 @@ export default class MessageComposerInput extends React.Component { } } + if (this.props.onInputStateChanged) { + let blockType = editorState.blocks.first().type; + console.log("onInputStateChanged; current block type is " + blockType + " and marks are " + editorState.activeMarks); + + if (blockType === 'list-item') { + const parent = editorState.document.getParent(editorState.blocks.first().key); + if (parent.type === 'numbered-list') { + blockType = 'numbered-list'; + } + else if (parent.type === 'bulleted-list') { + blockType = 'bulleted-list'; + } + } + const inputState = { + marks: editorState.activeMarks, + isRichTextEnabled: this.state.isRichTextEnabled, + blockType + }; + this.props.onInputStateChanged(inputState); + } + // Record the editor state for this room so that it can be retrieved after // switching to another room and back dis.dispatch({ @@ -1239,17 +1248,6 @@ export default class MessageComposerInput extends React.Component { await this.setDisplayedCompletion(null); // restore originalEditorState }; - /* returns inline style and block type of current SelectionState so MessageComposer can render formatting - buttons. */ - getSelectionInfo(editorState: Value) { - return { - marks: editorState.activeMarks, - // XXX: shouldn't we return all the types of blocks in the current selection, - // not just the anchor? - blockType: editorState.anchorBlock ? editorState.anchorBlock.type : null, - }; - } - /* If passed null, restores the original editor content from state.originalEditorState. * If passed a non-null displayedCompletion, modifies state.originalEditorState to compute new state.editorState. */ @@ -1406,18 +1404,22 @@ export default class MessageComposerInput extends React.Component { } }; - onFormatButtonClicked = (name: "bold" | "italic" | "strike" | "code" | "underline" | "quote" | "bullet" | "numbullet", e) => { - e.preventDefault(); // don't steal focus from the editor! + onFormatButtonClicked = (name, e) => { + if (e) { + e.preventDefault(); // don't steal focus from the editor! + } - const command = { - // code: 'code-block', // let's have the button do inline code for now - quote: 'block-quote', - bullet: 'bulleted-list', - numbullet: 'numbered-list', - underline: 'underlined', - strike: 'deleted', - }[name] || name; - this.handleKeyCommand(command); + // XXX: horrible evil hack to ensure the editor is focused so the act + // of focusing it doesn't then cancel the format button being pressed + if (document.activeElement && document.activeElement.className !== 'mx_MessageComposer_editor') { + this.refs.editor.focus(); + setTimeout(()=>{ + this.handleKeyCommand(name); + }, 500); // can't find any callback to hook this to. onFocus and onChange and willComponentUpdate fire too early. + return; + } + + this.handleKeyCommand(name); }; getAutocompleteQuery(editorState: Value) { From e460cf35e0c553825a5d3ee2f11b449249acdcbd Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 22:48:40 +0100 Subject: [PATCH 054/123] hide formatting bar for MD editor --- src/components/views/rooms/MessageComposer.js | 2 +- src/components/views/rooms/MessageComposerInput.js | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 4d00927767..28502c348d 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -354,7 +354,7 @@ export default class MessageComposer extends React.Component { } let formatBar; - if (this.state.showFormatting) { + if (this.state.showFormatting && this.state.inputState.isRichTextEnabled) { const {marks, blockType} = this.state.inputState; const formatButtons = ["bold", "italic", "deleted", "underlined", "code", "block-quote", "bulleted-list", "numbered-list"].map( (name) => { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 2d5e6d050d..2bb35c5656 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1405,9 +1405,7 @@ export default class MessageComposerInput extends React.Component { }; onFormatButtonClicked = (name, e) => { - if (e) { - e.preventDefault(); // don't steal focus from the editor! - } + e.preventDefault(); // don't steal focus from the editor! // XXX: horrible evil hack to ensure the editor is focused so the act // of focusing it doesn't then cancel the format button being pressed From b616fd025e56fe9b338c1bada5fb9c12ecb84c71 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 23:34:06 +0100 Subject: [PATCH 055/123] comment out all the tests for now --- src/components/views/rooms/MessageComposerInput.js | 2 +- test/components/views/rooms/MessageComposerInput-test.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 2bb35c5656..1f0a544246 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -557,7 +557,7 @@ export default class MessageComposerInput extends React.Component { if (this.props.onInputStateChanged) { let blockType = editorState.blocks.first().type; - console.log("onInputStateChanged; current block type is " + blockType + " and marks are " + editorState.activeMarks); + // console.log("onInputStateChanged; current block type is " + blockType + " and marks are " + editorState.activeMarks); if (blockType === 'list-item') { const parent = editorState.document.getParent(editorState.blocks.first().key); diff --git a/test/components/views/rooms/MessageComposerInput-test.js b/test/components/views/rooms/MessageComposerInput-test.js index 42921db975..708071df23 100644 --- a/test/components/views/rooms/MessageComposerInput-test.js +++ b/test/components/views/rooms/MessageComposerInput-test.js @@ -10,6 +10,7 @@ const MessageComposerInput = sdk.getComponent('views.rooms.MessageComposerInput' import MatrixClientPeg from '../../../../src/MatrixClientPeg'; import RoomMember from 'matrix-js-sdk'; +/* function addTextToDraft(text) { const components = document.getElementsByClassName('public-DraftEditor-content'); if (components && components.length) { @@ -300,3 +301,4 @@ describe('MessageComposerInput', () => { expect(spy.args[0][1].formatted_body).toEqual('Click here'); }); }); +*/ \ No newline at end of file From 4439a04689605f7244505915e3494a86954cf28c Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 May 2018 23:43:42 +0100 Subject: [PATCH 056/123] fix lint --- .eslintrc.js | 1 + src/ComposerHistoryManager.js | 16 +++++----------- src/autocomplete/CommandProvider.js | 3 +-- src/autocomplete/PlainWithPillsSerializer.js | 15 ++++++--------- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index bf423a1ad8..62d24ea707 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -95,6 +95,7 @@ module.exports = { "new-cap": ["warn"], "key-spacing": ["warn"], "prefer-const": ["warn"], + "arrow-parens": "off", // crashes currently: https://github.com/eslint/eslint/issues/6274 "generator-star-spacing": "off", diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index f997e1d1cd..e78fbcdc3b 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -16,11 +16,6 @@ limitations under the License. */ import { Value } from 'slate'; -import Html from 'slate-html-serializer'; -import Md from 'slate-md-serializer'; -import Plain from 'slate-plain-serializer'; -import * as RichText from './RichText'; -import Markdown from './Markdown'; import _clamp from 'lodash/clamp'; @@ -38,17 +33,17 @@ class HistoryItem { this.format = format; } - static fromJSON(obj): HistoryItem { + static fromJSON(obj: Object): HistoryItem { return new HistoryItem( Value.fromJSON(obj.value), - obj.format + obj.format, ); } toJSON(): Object { return { value: this.value.toJSON(), - format: this.format + format: this.format, }; } } @@ -67,10 +62,9 @@ export default class ComposerHistoryManager { for (; item = sessionStorage.getItem(`${this.prefix}[${this.currentIndex}]`); this.currentIndex++) { try { this.history.push( - HistoryItem.fromJSON(JSON.parse(item)) + HistoryItem.fromJSON(JSON.parse(item)), ); - } - catch (e) { + } catch (e) { console.warn("Throwing away unserialisable history", e); } } diff --git a/src/autocomplete/CommandProvider.js b/src/autocomplete/CommandProvider.js index 4f2aed3dc6..d56cefb021 100644 --- a/src/autocomplete/CommandProvider.js +++ b/src/autocomplete/CommandProvider.js @@ -132,8 +132,7 @@ export default class CommandProvider extends AutocompleteProvider { let results; if (command[0] == '/') { results = COMMANDS; - } - else { + } else { results = this.matcher.match(command[0]); } completions = results.map((result) => { diff --git a/src/autocomplete/PlainWithPillsSerializer.js b/src/autocomplete/PlainWithPillsSerializer.js index 7428241b05..c1194ae2e1 100644 --- a/src/autocomplete/PlainWithPillsSerializer.js +++ b/src/autocomplete/PlainWithPillsSerializer.js @@ -31,7 +31,7 @@ class PlainWithPillsSerializer { * @param {String} options.pillFormat - either 'md', 'plain', 'id' */ constructor(options = {}) { - let { + const { pillFormat = 'plain', } = options; this.pillFormat = pillFormat; @@ -46,7 +46,7 @@ class PlainWithPillsSerializer { * @return {String} */ serialize = value => { - return this._serializeNode(value.document) + return this._serializeNode(value.document); } /** @@ -61,8 +61,7 @@ class PlainWithPillsSerializer { (node.object == 'block' && Block.isBlockList(node.nodes)) ) { return node.nodes.map(this._serializeNode).join('\n'); - } - else if (node.type == 'emoji') { + } else if (node.type == 'emoji') { return node.data.get('emojiUnicode'); } else if (node.type == 'pill') { switch (this.pillFormat) { @@ -73,11 +72,9 @@ class PlainWithPillsSerializer { case 'id': return node.data.get('completionId') || node.data.get('completion'); } - } - else if (node.nodes) { + } else if (node.nodes) { return node.nodes.map(this._serializeNode).join(''); - } - else { + } else { return node.text; } } @@ -89,4 +86,4 @@ class PlainWithPillsSerializer { * @type {PlainWithPillsSerializer} */ -export default PlainWithPillsSerializer \ No newline at end of file +export default PlainWithPillsSerializer; From 7de45f8b7beb9e7069b643b02f8b9c904cb5aab4 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 21 May 2018 03:48:59 +0100 Subject: [PATCH 057/123] make quoting work --- src/HtmlUtils.js | 32 ++++-- .../views/context_menus/MessageContextMenu.js | 2 +- .../views/rooms/MessageComposerInput.js | 107 +++++++++++------- 3 files changed, 85 insertions(+), 56 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 4c1564297d..607686d46d 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -403,19 +403,22 @@ class TextHighlighter extends BaseHighlighter { } - /* turn a matrix event body into html - * - * content: 'content' of the MatrixEvent - * - * highlights: optional list of words to highlight, ordered by longest word first - * - * opts.highlightLink: optional href to add to highlighted words - * opts.disableBigEmoji: optional argument to disable the big emoji class. - * opts.stripReplyFallback: optional argument specifying the event is a reply and so fallback needs removing - */ +/* turn a matrix event body into html + * + * content: 'content' of the MatrixEvent + * + * highlights: optional list of words to highlight, ordered by longest word first + * + * opts.highlightLink: optional href to add to highlighted words + * opts.disableBigEmoji: optional argument to disable the big emoji class. + * opts.stripReplyFallback: optional argument specifying the event is a reply and so fallback needs removing + * opts.returnString: return an HTML string rather than JSX elements + * opts.emojiOne: optional param to do emojiOne (default true) + */ export function bodyToHtml(content, highlights, opts={}) { const isHtmlMessage = content.format === "org.matrix.custom.html" && content.formatted_body; + const doEmojiOne = opts.emojiOne === undefined ? true : opts.emojiOne; let bodyHasEmoji = false; let strippedBody; @@ -441,8 +444,9 @@ export function bodyToHtml(content, highlights, opts={}) { if (opts.stripReplyFallback && formattedBody) formattedBody = ReplyThread.stripHTMLReply(formattedBody); strippedBody = opts.stripReplyFallback ? ReplyThread.stripPlainReply(content.body) : content.body; - bodyHasEmoji = containsEmoji(isHtmlMessage ? formattedBody : content.body); - + if (doEmojiOne) { + bodyHasEmoji = containsEmoji(isHtmlMessage ? formattedBody : content.body); + } // Only generate safeBody if the message was sent as org.matrix.custom.html if (isHtmlMessage) { @@ -467,6 +471,10 @@ export function bodyToHtml(content, highlights, opts={}) { delete sanitizeHtmlParams.textFilter; } + if (opts.returnString) { + return isDisplayedWithHtml ? safeBody : strippedBody; + } + let emojiBody = false; if (!opts.disableBigEmoji && bodyHasEmoji) { EMOJI_REGEX.lastIndex = 0; diff --git a/src/components/views/context_menus/MessageContextMenu.js b/src/components/views/context_menus/MessageContextMenu.js index 99ec493ced..22c6f2aa70 100644 --- a/src/components/views/context_menus/MessageContextMenu.js +++ b/src/components/views/context_menus/MessageContextMenu.js @@ -179,7 +179,7 @@ module.exports = React.createClass({ onQuoteClick: function() { dis.dispatch({ action: 'quote', - text: this.props.eventTileOps.getInnerText(), + event: this.props.mxEvent, }); this.closeMenu(); }, diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 1f0a544246..eb4edfcfcb 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -21,7 +21,7 @@ import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import { Editor } from 'slate-react'; import { getEventTransfer } from 'slate-react'; -import { Value, Document, Event, Inline, Text, Range, Node } from 'slate'; +import { Value, Document, Event, Block, Inline, Text, Range, Node } from 'slate'; import Html from 'slate-html-serializer'; import Md from 'slate-md-serializer'; @@ -342,37 +342,44 @@ export default class MessageComposerInput extends React.Component { }); } break; -/* - case 'quote': { // old quoting, whilst rich quoting is in labs - /// XXX: Not doing rich-text quoting from formatted-body because draft-js - /// has regressed such that when links are quoted, errors are thrown. See - /// https://github.com/vector-im/riot-web/issues/4756. - const body = escape(payload.text); - if (body) { - let content = RichText.htmlToContentState(`
    ${body}
    `); - if (!this.state.isRichTextEnabled) { - content = ContentState.createFromText(RichText.stateToMarkdown(content)); - } + case 'quote': { + const html = HtmlUtils.bodyToHtml(payload.event.getContent(), null, { + returnString: true, + emojiOne: false, + }); + const fragment = this.html.deserialize(html); + // FIXME: do we want to put in a permalink to the original quote here? + // If so, what should be the format, and how do we differentiate it from replies? - const blockMap = content.getBlockMap(); - let startSelection = SelectionState.createEmpty(contentState.getFirstBlock().getKey()); - contentState = Modifier.splitBlock(contentState, startSelection); - startSelection = SelectionState.createEmpty(contentState.getFirstBlock().getKey()); - contentState = Modifier.replaceWithFragment(contentState, - startSelection, - blockMap); - startSelection = SelectionState.createEmpty(contentState.getFirstBlock().getKey()); - if (this.state.isRichTextEnabled) { - contentState = Modifier.setBlockType(contentState, startSelection, 'blockquote'); + const quote = Block.create('block-quote'); + if (this.state.isRichTextEnabled) { + let change = editorState.change(); + if (editorState.anchorText.text === '' && editorState.anchorBlock.nodes.size === 1) { + // replace the current block rather than split the block + change = change.replaceNodeByKey(editorState.anchorBlock.key, quote); } - let editorState = EditorState.push(this.state.editorState, contentState, 'insert-characters'); - editorState = EditorState.moveSelectionToEnd(editorState); - this.onEditorContentChanged(editorState); - editor.focus(); + else { + // insert it into the middle of the block (splitting it) + change = change.insertBlock(quote); + } + change = change.insertFragmentByKey(quote.key, 0, fragment.document) + .focus(); + this.onChange(change); + } + else { + let fragmentChange = fragment.change(); + fragmentChange.moveToRangeOf(fragment.document) + .wrapBlock(quote); + + // FIXME: handle pills and use commonmark rather than md-serialize + const md = this.md.serialize(fragmentChange.value); + let change = editorState.change() + .insertText(md + '\n\n') + .focus(); + this.onChange(change); } } break; -*/ } }; @@ -555,7 +562,7 @@ export default class MessageComposerInput extends React.Component { } } - if (this.props.onInputStateChanged) { + if (this.props.onInputStateChanged && editorState.blocks.size > 0) { let blockType = editorState.blocks.first().type; // console.log("onInputStateChanged; current block type is " + blockType + " and marks are " + editorState.activeMarks); @@ -740,17 +747,31 @@ export default class MessageComposerInput extends React.Component { .unwrapBlock('numbered-list'); return change; } - else if (editorState.anchorOffset == 0 && - (this.hasBlock('block-quote') || - this.hasBlock('heading1') || - this.hasBlock('heading2') || - this.hasBlock('heading3') || - this.hasBlock('heading4') || - this.hasBlock('heading5') || - this.hasBlock('heading6') || - this.hasBlock('code-block'))) - { - return change.setBlocks(DEFAULT_NODE); + else if (editorState.anchorOffset == 0 && editorState.isCollapsed) { + // turn blocks back into paragraphs + if ((this.hasBlock('block-quote') || + this.hasBlock('heading1') || + this.hasBlock('heading2') || + this.hasBlock('heading3') || + this.hasBlock('heading4') || + this.hasBlock('heading5') || + this.hasBlock('heading6') || + this.hasBlock('code-block'))) + { + return change.setBlocks(DEFAULT_NODE); + } + + // remove paragraphs entirely if they're nested + const parent = editorState.document.getParent(editorState.anchorBlock.key); + if (editorState.anchorOffset == 0 && + this.hasBlock('paragraph') && + parent.nodes.size == 1 && + parent.object !== 'document') + { + return change.replaceNodeByKey(editorState.anchorBlock.key, editorState.anchorText) + .collapseToEndOf(parent) + .focus(); + } } } return; @@ -1013,7 +1034,7 @@ export default class MessageComposerInput extends React.Component { if (!shouldSendHTML) { shouldSendHTML = !!editorState.document.findDescendant(node => { // N.B. node.getMarks() might be private? - return ((node.object === 'block' && node.type !== 'line') || + return ((node.object === 'block' && node.type !== 'paragraph') || (node.object === 'inline') || (node.object === 'text' && node.getMarks().size > 0)); }); @@ -1131,13 +1152,13 @@ export default class MessageComposerInput extends React.Component { // heuristic to handle tall emoji, pills, etc pushing the cursor away from the top // or bottom of the page. // XXX: is this going to break on large inline images or top-to-bottom scripts? - const EDGE_THRESHOLD = 8; + const EDGE_THRESHOLD = 15; let navigateHistory = false; if (up) { const scrollCorrection = editorNode.scrollTop; const distanceFromTop = cursorRect.top - editorRect.top + scrollCorrection; - //console.log(`Cursor distance from editor top is ${distanceFromTop}`); + console.log(`Cursor distance from editor top is ${distanceFromTop}`); if (distanceFromTop < EDGE_THRESHOLD) { navigateHistory = true; } @@ -1146,7 +1167,7 @@ export default class MessageComposerInput extends React.Component { const scrollCorrection = editorNode.scrollHeight - editorNode.clientHeight - editorNode.scrollTop; const distanceFromBottom = editorRect.bottom - cursorRect.bottom + scrollCorrection; - //console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); + console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); if (distanceFromBottom < EDGE_THRESHOLD) { navigateHistory = true; } From 11cea616615989157e335d7ea2e323ce7fc978f8 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 21 May 2018 12:28:08 +0100 Subject: [PATCH 058/123] refocus editor after clicking on autocompletes --- src/components/views/rooms/MessageComposerInput.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index eb4edfcfcb..6919a304c2 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1313,7 +1313,8 @@ export default class MessageComposerInput extends React.Component { if (range) { const change = editorState.change() .collapseToAnchor() - .moveOffsetsTo(range.start, range.end); + .moveOffsetsTo(range.start, range.end) + .focus(); editorState = change.value; } @@ -1321,12 +1322,14 @@ export default class MessageComposerInput extends React.Component { if (inline) { change = editorState.change() .insertInlineAtRange(editorState.selection, inline) - .insertText(suffix); + .insertText(suffix) + .focus(); } else { change = editorState.change() .insertTextAtRange(editorState.selection, completion) - .insertText(suffix); + .insertText(suffix) + .focus(); } editorState = change.value; From cace5e8bfcf4c83fd916798f176a9a45ed292f73 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 00:41:46 +0100 Subject: [PATCH 059/123] fix bug where selection breaks after inserting emoji --- src/components/views/rooms/MessageComposerInput.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 6919a304c2..b71aaa6a42 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -33,7 +33,6 @@ import PlainWithPillsSerializer from "../../../autocomplete/PlainWithPillsSerial // Entity} from 'draft-js'; import classNames from 'classnames'; -import escape from 'lodash/escape'; import Promise from 'bluebird'; import MatrixClientPeg from '../../../MatrixClientPeg'; @@ -507,6 +506,15 @@ export default class MessageComposerInput extends React.Component { } }); + // work around weird bug where inserting emoji via the macOS + // emoji picker can leave the selection stuck in the emoji's + // child text. This seems to happen due to selection getting + // moved in the normalisation phase after calculating these changes + if (editorState.document.getParent(editorState.anchorKey).type === 'emoji') { + change = change.collapseToStartOfNextText(); + editorState = change.value; + } + /* const currentBlock = editorState.getSelection().getStartKey(); const currentSelection = editorState.getSelection(); From e7a4ffaf4531ffec7399bf42508eee491ec69718 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 00:52:00 +0100 Subject: [PATCH 060/123] fix emojioneifying autoconverted emoji --- .../views/rooms/MessageComposerInput.js | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index b71aaa6a42..cb015dd0ba 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -482,6 +482,32 @@ export default class MessageComposerInput extends React.Component { } */ + if (editorState.startText !== null) { + const text = editorState.startText.text; + const currentStartOffset = editorState.startOffset; + + // Automatic replacement of plaintext emoji to Unicode emoji + if (SettingsStore.getValue('MessageComposerInput.autoReplaceEmoji')) { + // The first matched group includes just the matched plaintext emoji + const emojiMatch = REGEX_EMOJI_WHITESPACE.exec(text.slice(0, currentStartOffset)); + if (emojiMatch) { + // plaintext -> hex unicode + const emojiUc = asciiList[emojiMatch[1]]; + // hex unicode -> shortname -> actual unicode + const unicodeEmoji = shortnameToUnicode(EMOJI_UNICODE_TO_SHORTNAME[emojiUc]); + + const range = Range.create({ + anchorKey: editorState.selection.startKey, + anchorOffset: currentStartOffset - emojiMatch[1].length - 1, + focusKey: editorState.selection.startKey, + focusOffset: currentStartOffset - 1, + }); + change = change.insertTextAtRange(range, unicodeEmoji); + editorState = change.value; + } + } + } + // emojioneify any emoji // XXX: is getTextsAsArray a private API? @@ -544,31 +570,6 @@ export default class MessageComposerInput extends React.Component { editorState = EditorState.forceSelection(editorState, currentSelection); } */ - if (editorState.startText !== null) { - const text = editorState.startText.text; - const currentStartOffset = editorState.startOffset; - - // Automatic replacement of plaintext emoji to Unicode emoji - if (SettingsStore.getValue('MessageComposerInput.autoReplaceEmoji')) { - // The first matched group includes just the matched plaintext emoji - const emojiMatch = REGEX_EMOJI_WHITESPACE.exec(text.slice(0, currentStartOffset)); - if (emojiMatch) { - // plaintext -> hex unicode - const emojiUc = asciiList[emojiMatch[1]]; - // hex unicode -> shortname -> actual unicode - const unicodeEmoji = shortnameToUnicode(EMOJI_UNICODE_TO_SHORTNAME[emojiUc]); - - const range = Range.create({ - anchorKey: editorState.selection.startKey, - anchorOffset: currentStartOffset - emojiMatch[1].length - 1, - focusKey: editorState.selection.startKey, - focusOffset: currentStartOffset, - }); - change = change.insertTextAtRange(range, unicodeEmoji); - editorState = change.value; - } - } - } if (this.props.onInputStateChanged && editorState.blocks.size > 0) { let blockType = editorState.blocks.first().type; From 794a60b9f8976452add3885f5a07af7f6f5362e4 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 01:36:21 +0100 Subject: [PATCH 061/123] refocus editor immediately after executing commands and persist selections correctly across blur/focus --- .../views/rooms/MessageComposerInput.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index cb015dd0ba..3a0329c399 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1005,12 +1005,13 @@ export default class MessageComposerInput extends React.Component { this.historyManager.save(editorState, this.state.isRichTextEnabled ? 'rich' : 'markdown'); this.setState({ editorState: this.createEditorState(), + }, ()=>{ + this.refs.editor.focus(); }); } if (cmd.promise) { cmd.promise.then(()=>{ console.log("Command success."); - this.refs.editor.focus(); }, (err)=>{ console.error("Command failure: %s", err); const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); @@ -1499,6 +1500,18 @@ export default class MessageComposerInput extends React.Component { this.handleKeyCommand('toggle-mode'); }; + onBlur = (e) => { + this.selection = this.state.editorState.selection; + }; + + onFocus = (e) => { + if (this.selection) { + const change = this.state.editorState.change().select(this.selection); + this.onChange(change); + delete this.selection; + } + }; + render() { const activeEditorState = this.state.originalEditorState || this.state.editorState; @@ -1532,6 +1545,8 @@ export default class MessageComposerInput extends React.Component { onChange={this.onChange} onKeyDown={this.onKeyDown} onPaste={this.onPaste} + onBlur={this.onBlur} + onFocus={this.onFocus} renderNode={this.renderNode} renderMark={this.renderMark} spellCheck={true} From 6ac23248745d9303f0cdc7ce62ca94c018a1c6ba Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 01:43:03 +0100 Subject: [PATCH 062/123] fix wordwrap and pre formatting --- res/css/views/rooms/_MessageComposer.scss | 27 ++--------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/res/css/views/rooms/_MessageComposer.scss b/res/css/views/rooms/_MessageComposer.scss index 72d31cfddd..3eb445d239 100644 --- a/res/css/views/rooms/_MessageComposer.scss +++ b/res/css/views/rooms/_MessageComposer.scss @@ -89,6 +89,7 @@ limitations under the License. max-height: 120px; min-height: 19px; overflow: auto; + word-break: break-word; } // FIXME: rather unpleasant hack to get rid of

    margins. @@ -110,30 +111,6 @@ limitations under the License. animation: 0.2s visualbell; } -.mx_MessageComposer_input_empty .public-DraftEditorPlaceholder-root { - display: none; -} - -/* -.mx_MessageComposer_input .DraftEditor-root { - width: 100%; - flex: 1; - word-break: break-word; - max-height: 120px; - min-height: 21px; - overflow: auto; -} -*/ - -.mx_MessageComposer_input .DraftEditor-root .DraftEditor-editorContainer { - /* Ensure mx_UserPill and mx_RoomPill (see _RichText) are not obscured from the top */ - padding-top: 2px; -} - -.mx_MessageComposer .public-DraftStyleDefault-block { - overflow-x: hidden; -} - .mx_MessageComposer_input blockquote { color: $blockquote-fg-color; margin: 0 0 16px; @@ -141,7 +118,7 @@ limitations under the License. border-left: 4px solid $blockquote-bar-color; } -.mx_MessageComposer_input pre.public-DraftStyleDefault-pre pre { +.mx_MessageComposer_input pre { background-color: $rte-code-bg-color; border-radius: 3px; padding: 10px; From 6fba8311f9af31bee30eadd188d655a38f11478c Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 02:09:30 +0100 Subject: [PATCH 063/123] escape blockquotes correctly and fix NPE --- src/components/views/rooms/MessageComposerInput.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 3a0329c399..4b41c76076 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -184,7 +184,7 @@ export default class MessageComposerInput extends React.Component { // TODO: this can probably be more robust - it doesn't consider // indenting or lists for instance. return children.replace(/([*_~`+])/g, '\\$1') - .replace(/^([>#\|])/g, '\\$1'); + .replace(/^([>#\|])/mg, '\\$1'); } else if (obj.object === 'inline') { switch (obj.type) { @@ -369,6 +369,7 @@ export default class MessageComposerInput extends React.Component { let fragmentChange = fragment.change(); fragmentChange.moveToRangeOf(fragment.document) .wrapBlock(quote); + //.setBlocks('block-quote'); // FIXME: handle pills and use commonmark rather than md-serialize const md = this.md.serialize(fragmentChange.value); @@ -536,7 +537,9 @@ export default class MessageComposerInput extends React.Component { // emoji picker can leave the selection stuck in the emoji's // child text. This seems to happen due to selection getting // moved in the normalisation phase after calculating these changes - if (editorState.document.getParent(editorState.anchorKey).type === 'emoji') { + if (editorState.anchorKey && + editorState.document.getParent(editorState.anchorKey).type === 'emoji') + { change = change.collapseToStartOfNextText(); editorState = change.value; } From fc1c4996fc80bd841dc3dd14165d504bdcbd6707 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 02:15:34 +0100 Subject: [PATCH 064/123] slate-md-serializer 3.1.0 now escapes correctly --- package.json | 2 +- src/components/views/rooms/MessageComposerInput.js | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index dfc02a7e26..eea7a6857f 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "^3.0.3", + "slate-md-serializer": "^3.1.0", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 4b41c76076..0b57e90184 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -178,15 +178,7 @@ export default class MessageComposerInput extends React.Component { rules: [ { serialize: (obj, children) => { - if (obj.object === 'string') { - // escape any MD in it. i have no idea why the serializer doesn't - // do this already. - // TODO: this can probably be more robust - it doesn't consider - // indenting or lists for instance. - return children.replace(/([*_~`+])/g, '\\$1') - .replace(/^([>#\|])/mg, '\\$1'); - } - else if (obj.object === 'inline') { + if (obj.object === 'inline') { switch (obj.type) { case 'pill': return `[${ obj.data.get('completion') }](${ obj.data.get('href') })`; From edc8264cff0ee7110d786d76961b59a55830380c Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 02:48:57 +0100 Subject: [PATCH 065/123] shift to custom slate-md-serializer --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eea7a6857f..542f6c7cc2 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "^3.1.0", + "slate-md-serializer": "matrix-org/slate-md-serializer#50a133a8", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", From 6f3634c33f9d0305d79770b08c0daa01edca0d13 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 03:08:55 +0100 Subject: [PATCH 066/123] bump slate-md-serializer --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 542f6c7cc2..63022f6c9d 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "matrix-org/slate-md-serializer#50a133a8", + "slate-md-serializer": "matrix-org/slate-md-serializer#d6a7652", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", From a822a1c9a07d7d83ebc7b39149e3d67188dbfea8 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 03:19:33 +0100 Subject: [PATCH 067/123] bump dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 63022f6c9d..f45d3a4d95 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "flux": "2.1.1", "focus-trap-react": "^3.0.5", "fuse.js": "^2.2.0", - "gemini-scrollbar": "matrix-org/gemini-scrollbar#b302279", + "gemini-scrollbar": "matrix-org/gemini-scrollbar#926a4c7", "gfm.css": "^1.1.1", "glob": "^5.0.14", "highlight.js": "^9.0.0", From 079b1238a1954fbf096627b10eb58bc378461e68 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 03:20:59 +0100 Subject: [PATCH 068/123] bump right dep --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f45d3a4d95..79a3bf80e1 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "flux": "2.1.1", "focus-trap-react": "^3.0.5", "fuse.js": "^2.2.0", - "gemini-scrollbar": "matrix-org/gemini-scrollbar#926a4c7", + "gemini-scrollbar": "matrix-org/gemini-scrollbar#b302279", "gfm.css": "^1.1.1", "glob": "^5.0.14", "highlight.js": "^9.0.0", @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "matrix-org/slate-md-serializer#d6a7652", + "slate-md-serializer": "matrix-org/slate-md-serializer#926a4c7", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", From 95bfface99a917aaa466873ab3b050f9990dae68 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 03:43:40 +0100 Subject: [PATCH 069/123] comment out broken logic for stripping p tags & bump dep --- package.json | 2 +- src/Markdown.js | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 79a3bf80e1..33bc75eff8 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "matrix-org/slate-md-serializer#926a4c7", + "slate-md-serializer": "matrix-org/slate-md-serializer#1635f37", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", diff --git a/src/Markdown.js b/src/Markdown.js index e67f4df4fd..2b16800d85 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -102,6 +102,7 @@ export default class Markdown { // (https://github.com/vector-im/riot-web/issues/3154) softbreak: '
    ', }); +/* const real_paragraph = renderer.paragraph; renderer.paragraph = function(node, entering) { @@ -114,16 +115,21 @@ export default class Markdown { real_paragraph.call(this, node, entering); } }; +*/ renderer.html_inline = html_if_tag_allowed; + renderer.html_block = function(node) { +/* // as with `paragraph`, we only insert line breaks // if there are multiple lines in the markdown. const isMultiLine = is_multi_line(node); - if (isMultiLine) this.cr(); +*/ html_if_tag_allowed.call(this, node); +/* if (isMultiLine) this.cr(); +*/ }; return renderer.render(this.parsed); @@ -150,6 +156,7 @@ export default class Markdown { this.lit(s); }; +/* renderer.paragraph = function(node, entering) { // as with toHTML, only append lines to paragraphs if there are // multiple paragraphs @@ -159,10 +166,12 @@ export default class Markdown { } } }; + renderer.html_block = function(node) { this.lit(node.literal); if (is_multi_line(node) && node.next) this.lit('\n\n'); }; +*/ // convert MD links into console-friendly ' < http://foo >' style links // ...except given this function never gets called with links, it's useless. From f40af434bcfeec7e37015192993adc639b54d79e Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 03:46:53 +0100 Subject: [PATCH 070/123] unbreak Markdown.toPlaintext --- src/Markdown.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Markdown.js b/src/Markdown.js index 2b16800d85..e02118397b 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -156,7 +156,6 @@ export default class Markdown { this.lit(s); }; -/* renderer.paragraph = function(node, entering) { // as with toHTML, only append lines to paragraphs if there are // multiple paragraphs @@ -171,7 +170,6 @@ export default class Markdown { this.lit(node.literal); if (is_multi_line(node) && node.next) this.lit('\n\n'); }; -*/ // convert MD links into console-friendly ' < http://foo >' style links // ...except given this function never gets called with links, it's useless. From 5b4a036e8c7db150aae1f404cb952ecb49d5970f Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 03:53:05 +0100 Subject: [PATCH 071/123] bump dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 33bc75eff8..fb0fd3ec90 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "matrix-org/slate-md-serializer#1635f37", + "slate-md-serializer": "matrix-org/slate-md-serializer#f5deb3f", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", From 87c3e92014754ff454f07cd642cb456dc19dd43f Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 13:32:48 +0100 Subject: [PATCH 072/123] bump dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fb0fd3ec90..5dc3e698f2 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "matrix-org/slate-md-serializer#f5deb3f", + "slate-md-serializer": "matrix-org/slate-md-serializer#11fe1b6", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", From fb5240aabd67a6a6f07d85b5f003c757e38b95dd Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 14:00:54 +0100 Subject: [PATCH 073/123] explain why we're now including

    s --- src/Markdown.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Markdown.js b/src/Markdown.js index e02118397b..9d9a8621c9 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -102,6 +102,15 @@ export default class Markdown { // (https://github.com/vector-im/riot-web/issues/3154) softbreak: '
    ', }); + + // Trying to strip out the wrapping

    causes a lot more complication + // than it's worth, i think. For instance, this code will go and strip + // out any

    tag (no matter where it is in the tree) which doesn't + // contain \n's. + // On the flip side,

    s are quite opionated and restricted on where + // you can nest them. + // + // Let's try sending with

    s anyway for now, though. /* const real_paragraph = renderer.paragraph; From b0ff61f7ef3757dee9f6dc9ee0881b7868d6c015 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 14:12:08 +0100 Subject: [PATCH 074/123] switch code schema to match slate-md-serializer --- .../views/rooms/MessageComposerInput.js | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 0b57e90184..94ef88d04b 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -95,7 +95,7 @@ const BLOCK_TAGS = { h6: 'heading6', li: 'list-item', ol: 'numbered-list', - pre: 'code-block', + pre: 'code', }; const MARK_TAGS = { @@ -103,7 +103,7 @@ const MARK_TAGS = { b: 'bold', // deprecated em: 'italic', i: 'italic', // deprecated - code: 'code', + code: 'inline-code', u: 'underlined', del: 'deleted', strike: 'deleted', // deprecated @@ -710,7 +710,7 @@ export default class MessageComposerInput extends React.Component { [KeyCode.KEY_B]: 'bold', [KeyCode.KEY_I]: 'italic', [KeyCode.KEY_U]: 'underlined', - [KeyCode.KEY_J]: 'code', + [KeyCode.KEY_J]: 'inline-code', }[ev.keyCode]; if (ctrlCmdCommand) { @@ -760,7 +760,7 @@ export default class MessageComposerInput extends React.Component { this.hasBlock('heading4') || this.hasBlock('heading5') || this.hasBlock('heading6') || - this.hasBlock('code-block'))) + this.hasBlock('code'))) { return change.setBlocks(DEFAULT_NODE); } @@ -832,7 +832,7 @@ export default class MessageComposerInput extends React.Component { case 'heading5': case 'heading6': case 'list-item': - case 'code-block': { + case 'code': { const isActive = this.hasBlock(type); const isList = this.hasBlock('list-item'); @@ -850,7 +850,7 @@ export default class MessageComposerInput extends React.Component { // marks: case 'bold': case 'italic': - case 'code': + case 'inline-code': case 'underlined': case 'deleted': { change.toggleMark(type); @@ -885,8 +885,8 @@ export default class MessageComposerInput extends React.Component { 'underline': (text) => `${text}`, 'strike': (text) => `${text}`, // ("code" is triggered by ctrl+j by draft-js by default) - 'code': (text) => treatInlineCodeAsBlock ? textMdCodeBlock(text) : `\`${text}\``, - 'code-block': textMdCodeBlock, + 'inline-code': (text) => treatInlineCodeAsBlock ? textMdCodeBlock(text) : `\`${text}\``, + 'code': textMdCodeBlock, 'blockquote': (text) => text.split('\n').map((line) => `> ${line}\n`).join('') + '\n', 'unordered-list-item': (text) => text.split('\n').map((line) => `\n- ${line}`).join(''), 'ordered-list-item': (text) => text.split('\n').map((line, i) => `\n${i + 1}. ${line}`).join(''), @@ -897,8 +897,8 @@ export default class MessageComposerInput extends React.Component { 'italic': -1, 'underline': -4, 'strike': -6, - 'code': treatInlineCodeAsBlock ? -5 : -1, - 'code-block': -5, + 'inline-code': treatInlineCodeAsBlock ? -5 : -1, + 'code': -5, 'blockquote': -2, }[command]; @@ -971,7 +971,7 @@ export default class MessageComposerInput extends React.Component { } if (this.state.editorState.blocks.some( - block => ['code-block', 'block-quote', 'list-item'].includes(block.type) + block => ['code', 'block-quote', 'list-item'].includes(block.type) )) { // allow the user to terminate blocks by hitting return rather than sending a msg return; @@ -1369,7 +1369,7 @@ export default class MessageComposerInput extends React.Component { return

  • {children}
  • ; case 'numbered-list': return
      {children}
    ; - case 'code-block': + case 'code': return
    {children}
    ; case 'link': return {children}; @@ -1424,7 +1424,7 @@ export default class MessageComposerInput extends React.Component { return {children}; case 'italic': return {children}; - case 'code': + case 'inline-code': return {children}; case 'underlined': return {children}; From 294565c47e67eb207900e45cd7355f069f8ae888 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 14:12:36 +0100 Subject: [PATCH 075/123] switch code schema to match slate-md-serializer --- .../{button-text-code-on.svg => button-text-inline-code-on.svg} | 0 res/img/{button-text-code.svg => button-text-inline-code.svg} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename res/img/{button-text-code-on.svg => button-text-inline-code-on.svg} (100%) rename res/img/{button-text-code.svg => button-text-inline-code.svg} (100%) diff --git a/res/img/button-text-code-on.svg b/res/img/button-text-inline-code-on.svg similarity index 100% rename from res/img/button-text-code-on.svg rename to res/img/button-text-inline-code-on.svg diff --git a/res/img/button-text-code.svg b/res/img/button-text-inline-code.svg similarity index 100% rename from res/img/button-text-code.svg rename to res/img/button-text-inline-code.svg From 864a33f978dd313b23064f55eb13039ad3c1a5d3 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 23 May 2018 20:12:13 +0100 Subject: [PATCH 076/123] switch to using 'code' for both blocks & marks to match md-serializer --- package.json | 2 +- src/components/views/rooms/MessageComposer.js | 2 +- src/components/views/rooms/MessageComposerInput.js | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 5dc3e698f2..f3f184fb65 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "matrix-org/slate-md-serializer#11fe1b6", + "slate-md-serializer": "matrix-org/slate-md-serializer#1580ed67", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 28502c348d..686ab3fcb6 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -356,7 +356,7 @@ export default class MessageComposer extends React.Component { let formatBar; if (this.state.showFormatting && this.state.inputState.isRichTextEnabled) { const {marks, blockType} = this.state.inputState; - const formatButtons = ["bold", "italic", "deleted", "underlined", "code", "block-quote", "bulleted-list", "numbered-list"].map( + const formatButtons = ["bold", "italic", "deleted", "underlined", "inline-code", "block-quote", "bulleted-list", "numbered-list"].map( (name) => { const active = marks.some(mark => mark.type === name) || blockType === name; const suffix = active ? '-on' : ''; diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 94ef88d04b..50b3422fee 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -103,7 +103,7 @@ const MARK_TAGS = { b: 'bold', // deprecated em: 'italic', i: 'italic', // deprecated - code: 'inline-code', + code: 'code', u: 'underlined', del: 'deleted', strike: 'deleted', // deprecated @@ -853,7 +853,7 @@ export default class MessageComposerInput extends React.Component { case 'inline-code': case 'underlined': case 'deleted': { - change.toggleMark(type); + change.toggleMark(type === 'inline-code' ? 'code' : type); } break; @@ -885,7 +885,7 @@ export default class MessageComposerInput extends React.Component { 'underline': (text) => `${text}`, 'strike': (text) => `${text}`, // ("code" is triggered by ctrl+j by draft-js by default) - 'inline-code': (text) => treatInlineCodeAsBlock ? textMdCodeBlock(text) : `\`${text}\``, + 'code': (text) => treatInlineCodeAsBlock ? textMdCodeBlock(text) : `\`${text}\``, 'code': textMdCodeBlock, 'blockquote': (text) => text.split('\n').map((line) => `> ${line}\n`).join('') + '\n', 'unordered-list-item': (text) => text.split('\n').map((line) => `\n- ${line}`).join(''), @@ -897,7 +897,7 @@ export default class MessageComposerInput extends React.Component { 'italic': -1, 'underline': -4, 'strike': -6, - 'inline-code': treatInlineCodeAsBlock ? -5 : -1, + 'code': treatInlineCodeAsBlock ? -5 : -1, 'code': -5, 'blockquote': -2, }[command]; @@ -1424,7 +1424,7 @@ export default class MessageComposerInput extends React.Component { return {children}; case 'italic': return {children}; - case 'inline-code': + case 'code': return {children}; case 'underlined': return {children}; From ab412122580456fc2ec9011f41d14bb7fcf402e1 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 26 May 2018 00:37:21 +0100 Subject: [PATCH 077/123] fix double-nested code blocks & bump md-serializer --- package.json | 2 +- src/components/views/rooms/MessageComposerInput.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f3f184fb65..a65b6e2640 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "slate": "^0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", - "slate-md-serializer": "matrix-org/slate-md-serializer#1580ed67", + "slate-md-serializer": "matrix-org/slate-md-serializer#f7c4ad3", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 50b3422fee..f1a4fd5140 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1370,7 +1370,7 @@ export default class MessageComposerInput extends React.Component { case 'numbered-list': return
      {children}
    ; case 'code': - return
    {children}
    ; + return
    {children}
    ; case 'link': return {children}; case 'pill': { From 6299e188a4bcd680087822b59dfa3d6ab9bb30e1 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 26 May 2018 02:11:20 +0100 Subject: [PATCH 078/123] unbreak keyboard shortcuts & ctrl-backspace --- src/components/views/rooms/MessageComposerInput.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index f1a4fd5140..d2730027d1 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -716,7 +716,7 @@ export default class MessageComposerInput extends React.Component { if (ctrlCmdCommand) { return this.handleKeyCommand(ctrlCmdCommand); } - return false; + return; } switch (ev.keyCode) { @@ -739,6 +739,10 @@ export default class MessageComposerInput extends React.Component { }; onBackspace = (ev: Event, change: Change): Change => { + if (ev.ctrlKey || ev.metaKey || ev.altKey || ev.ctrlKey || ev.shiftKey) { + return; + } + if (this.state.isRichTextEnabled) { // let backspace exit lists const isList = this.hasBlock('list-item'); From c6837af398fcddfc8a409962ebb29ccb73ed5995 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 3 Jul 2018 21:36:31 +0100 Subject: [PATCH 079/123] import-type Change from slate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit 85ed499) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index eed6b18aa4..f5b22fd481 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -22,6 +22,7 @@ import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import { Editor } from 'slate-react'; import { getEventTransfer } from 'slate-react'; import { Value, Document, Event, Block, Inline, Text, Range, Node } from 'slate'; +import type { Change } from 'slate'; import Html from 'slate-html-serializer'; import Md from 'slate-md-serializer'; From 372fa29ad31820b8a96c221bffb222634310cfd9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 3 Jul 2018 23:36:59 +0100 Subject: [PATCH 080/123] take edge into consideration when moving focus region on arrow keys fixes: >Pressing right when the caret is immediately left of some entity (pill, emojione emoji, etc..) causes the caret to jump to the left of the next entity (or end of the message if there are no more entities) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit 0982617) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index f5b22fd481..7ccee3ab16 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -449,13 +449,13 @@ export default class MessageComposerInput extends React.Component { if (this.direction !== '') { const focusedNode = editorState.focusInline || editorState.focusText; if (focusedNode.isVoid) { + const edge = this.direction === 'Previous' ? 'End' : 'Start'; if (editorState.isCollapsed) { - change = change[`collapseToEndOf${ this.direction }Text`](); - } - else { + change = change[`collapseTo${ edge }Of${ this.direction }Text`](); + } else { const block = this.direction === 'Previous' ? editorState.previousText : editorState.nextText; if (block) { - change = change.moveFocusToEndOf(block) + change = change[`moveFocusTo${ edge }Of`](block); } } editorState = change.value; From 483116fb0382d6e8710c9ea54e23e59e8c4ac232 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Jul 2018 00:04:56 +0100 Subject: [PATCH 081/123] add rule to slate-md-serializer: make underlined and removed work for CM Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit b521efd) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 7ccee3ab16..6f2568d3b3 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -178,17 +178,27 @@ export default class MessageComposerInput extends React.Component { rules: [ { serialize: (obj, children) => { - if (obj.object === 'inline') { - switch (obj.type) { - case 'pill': - return `[${ obj.data.get('completion') }](${ obj.data.get('href') })`; - case 'emoji': - return obj.data.get('emojiUnicode'); - } + if (obj.object !== 'inline') return; + switch (obj.type) { + case 'pill': + return `[${ obj.data.get('completion') }](${ obj.data.get('href') })`; + case 'emoji': + return obj.data.get('emojiUnicode'); } - } - } - ] + }, + }, { + serialize: (obj, children) => { + if (obj.object !== 'mark') return; + // XXX: slate-md-serializer consumes marks other than bold, italic, code, inserted, deleted + switch (obj.type) { + case 'underlined': + return `${ children }`; + case 'deleted': + return `${ children }`; + } + }, + }, + ], }); this.html = new Html({ @@ -633,6 +643,7 @@ export default class MessageComposerInput extends React.Component { // FIXME: this conversion loses pills (turning them into pure MD links). // We need to add a pill-aware deserialize method // to PlainWithPillsSerializer which recognises pills in raw MD and turns them into pills. + debugger; return Plain.deserialize( // FIXME: we compile the MD out of the RTE state using slate-md-serializer // which doesn't roundtrip symmetrically with commonmark, which we use for @@ -688,7 +699,7 @@ export default class MessageComposerInput extends React.Component { return editorState.blocks.some(node => node.type == type) }; - onKeyDown = (ev: Event, change: Change, editor: Editor) => { + onKeyDown = (ev: KeyboardEvent, change: Change, editor: Editor) => { this.suppressAutoComplete = false; @@ -732,12 +743,21 @@ export default class MessageComposerInput extends React.Component { return this.onTab(ev); case KeyCode.ESCAPE: return this.onEscape(ev); + case KeyCode.SPACE: + return this.onSpace(ev, change); default: // don't intercept it return; } }; + onSpace = (ev: KeyboardEvent, change: Change): Change => { + // drop a point in history so the user can undo a word + // XXX: this seems nasty but adding to history manually seems a no-go + ev.preventDefault(); + return change.setOperationFlag("skip", false).setOperationFlag("merge", false).insertText(ev.key); + }; + onBackspace = (ev: Event, change: Change): Change => { if (ev.ctrlKey || ev.metaKey || ev.altKey || ev.ctrlKey || ev.shiftKey) { return; From faf17f06c6114cfd78e6573fb908654faac6cf42 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Jul 2018 00:15:36 +0100 Subject: [PATCH 082/123] remove debugger statement Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit b6f7940) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 6f2568d3b3..67602e5f4a 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -643,7 +643,6 @@ export default class MessageComposerInput extends React.Component { // FIXME: this conversion loses pills (turning them into pure MD links). // We need to add a pill-aware deserialize method // to PlainWithPillsSerializer which recognises pills in raw MD and turns them into pills. - debugger; return Plain.deserialize( // FIXME: we compile the MD out of the RTE state using slate-md-serializer // which doesn't roundtrip symmetrically with commonmark, which we use for From 43204ea1772e258efd70b74e81b5fffd36ee4e52 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Jul 2018 10:17:05 +0100 Subject: [PATCH 083/123] fix Control-Backspace after select-all Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit 0f32ec0) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 67602e5f4a..dc7cbf0b34 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -713,22 +713,6 @@ export default class MessageComposerInput extends React.Component { this.direction = ''; } - if (isOnlyCtrlOrCmdKeyEvent(ev)) { - const ctrlCmdCommand = { - // C-m => Toggles between rich text and markdown modes - [KeyCode.KEY_M]: 'toggle-mode', - [KeyCode.KEY_B]: 'bold', - [KeyCode.KEY_I]: 'italic', - [KeyCode.KEY_U]: 'underlined', - [KeyCode.KEY_J]: 'inline-code', - }[ev.keyCode]; - - if (ctrlCmdCommand) { - return this.handleKeyCommand(ctrlCmdCommand); - } - return; - } - switch (ev.keyCode) { case KeyCode.ENTER: return this.handleReturn(ev, change); @@ -744,9 +728,21 @@ export default class MessageComposerInput extends React.Component { return this.onEscape(ev); case KeyCode.SPACE: return this.onSpace(ev, change); - default: - // don't intercept it - return; + } + + if (isOnlyCtrlOrCmdKeyEvent(ev)) { + const ctrlCmdCommand = { + // C-m => Toggles between rich text and markdown modes + [KeyCode.KEY_M]: 'toggle-mode', + [KeyCode.KEY_B]: 'bold', + [KeyCode.KEY_I]: 'italic', + [KeyCode.KEY_U]: 'underlined', + [KeyCode.KEY_J]: 'inline-code', + }[ev.keyCode]; + + if (ctrlCmdCommand) { + return this.handleKeyCommand(ctrlCmdCommand); + } } }; @@ -757,15 +753,22 @@ export default class MessageComposerInput extends React.Component { return change.setOperationFlag("skip", false).setOperationFlag("merge", false).insertText(ev.key); }; - onBackspace = (ev: Event, change: Change): Change => { - if (ev.ctrlKey || ev.metaKey || ev.altKey || ev.ctrlKey || ev.shiftKey) { + onBackspace = (ev: KeyboardEvent, change: Change): Change => { + if (ev.metaKey || ev.altKey || ev.shiftKey) { return; } + const { editorState } = this.state; + + // Allow Ctrl/Cmd-Backspace when focus starts at the start of the composer (e.g select-all) + // for some reason if slate sees you Ctrl-backspace and your anchorOffset=0 it just resets your focus + if (!editorState.isCollapsed && editorState.anchorOffset === 0) { + return change.delete(); + } + if (this.state.isRichTextEnabled) { // let backspace exit lists const isList = this.hasBlock('list-item'); - const { editorState } = this.state; if (isList && editorState.anchorOffset == 0) { change From 5b74c615ae8c8cfb4693795695e76c5bde9e05cc Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Jul 2018 10:54:38 +0100 Subject: [PATCH 084/123] add missing import Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit 47b6099) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index dc7cbf0b34..3ef10ca79e 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -55,7 +55,7 @@ import Markdown from '../../../Markdown'; import ComposerHistoryManager from '../../../ComposerHistoryManager'; import MessageComposerStore from '../../../stores/MessageComposerStore'; -import {MATRIXTO_MD_LINK_PATTERN} from '../../../linkify-matrix'; +import {MATRIXTO_MD_LINK_PATTERN, MATRIXTO_URL_PATTERN} from '../../../linkify-matrix'; const REGEX_MATRIXTO_MARKDOWN_GLOBAL = new RegExp(MATRIXTO_MD_LINK_PATTERN, 'g'); import {asciiRegexp, unicodeRegexp, shortnameToUnicode, emojioneList, asciiList, mapUnicodeToShort, toShort} from 'emojione'; From 5bd4104c9650434b87cffbf2681793551dd62ea7 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Jul 2018 12:16:11 +0100 Subject: [PATCH 085/123] modify ComposerHistoryManager Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit d139dd6) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/ComposerHistoryManager.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index e78fbcdc3b..0164e6c4cd 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -51,8 +51,8 @@ class HistoryItem { export default class ComposerHistoryManager { history: Array = []; prefix: string; - lastIndex: number = 0; - currentIndex: number = 0; + lastIndex: number = 0; // used for indexing the storage + currentIndex: number = 0; // used for indexing the loaded validated history Array constructor(roomId: string, prefix: string = 'mx_composer_history_') { this.prefix = prefix + roomId; @@ -69,18 +69,19 @@ export default class ComposerHistoryManager { } } this.lastIndex = this.currentIndex; + // reset currentIndex to account for any unserialisable history + this.currentIndex = this.history.length; } save(value: Value, format: MessageFormat) { const item = new HistoryItem(value, format); this.history.push(item); - this.currentIndex = this.lastIndex + 1; + this.currentIndex = this.history.length; sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item.toJSON())); } getItem(offset: number): ?HistoryItem { - this.currentIndex = _clamp(this.currentIndex + offset, 0, this.lastIndex - 1); - const item = this.history[this.currentIndex]; - return item; + this.currentIndex = _clamp(this.currentIndex + offset, 0, this.history.length - 1); + return this.history[this.currentIndex]; } } From 8665f10f275f5b735e180fa781de53af801149ee Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 8 Jul 2018 23:08:02 +0100 Subject: [PATCH 086/123] pin slate to 0.33.4 to avoid https://github.com/ianstormtaylor/slate/pull/1958 (cherry picked from commit 445faca) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d639a4e51c..661febf6af 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "react-beautiful-dnd": "^4.0.1", "react-dom": "^15.6.0", "react-gemini-scrollbar": "matrix-org/react-gemini-scrollbar#5e97aef", - "slate": "^0.33.4", + "slate": "0.33.4", "slate-react": "^0.12.4", "slate-html-serializer": "^0.6.1", "slate-md-serializer": "matrix-org/slate-md-serializer#f7c4ad3", From 83f26149197b17ba3af77ec5b0263c16d6da6f3e Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 9 Jul 2018 00:51:48 +0100 Subject: [PATCH 087/123] add guide to slate's data formats and how we convert (cherry picked from commit e7e4ee8) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- docs/slate-formats.md | 88 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 docs/slate-formats.md diff --git a/docs/slate-formats.md b/docs/slate-formats.md new file mode 100644 index 0000000000..b526109c1b --- /dev/null +++ b/docs/slate-formats.md @@ -0,0 +1,88 @@ +Guide to data types used by the Slate-based Rich Text Editor +------------------------------------------------------------ + +We always store the Slate editor state in its Value form. + +The schema for the Value is the same whether the editor is in MD or rich text mode, and is currently (rather arbitrarily) +dictated by the schema expected by slate-md-serializer, simply because it was the only bit of the pipeline which +has opinions on the schema. (slate-html-serializer lets you define how to serialize whatever schema you like). + +The BLOCK_TAGS and MARK_TAGS give the mapping from HTML tags to the schema's node types (for blocks, which describe +block content like divs, and marks, which describe inline formatted sections like spans). + +We use

    as the parent tag for the message (XXX: although some tags are technically not allowed to be nested within p's) + +Various conversions are performed as content is moved between HTML, MD, and plaintext representations of HTML and MD. + +The primitives used are: + + * Markdown.js - models commonmark-formatted MD strings (as entered by the composer in MD mode) + * toHtml() - renders them to HTML suitable for sending on the wire + * isPlainText() - checks whether the parsed MD contains anything other than simple text. + * toPlainText() - renders MD to plain text in order to remove backslashes. Only works if the MD is already plaintext (otherwise it just emits HTML) + + * slate-html-serializer + * converts Values to HTML (serialising) using our schema rules + * converts HTML to Values (deserialising) using our schema rules + + * slate-md-serializer + * converts rich Values to MD strings (serialising) but using a non-commonmark generic MD dialect. + * This should use commonmark, but we use the serializer here for expedience rather than writing a commonmark one. + + * slate-plain-serializer + * converts Values to plain text strings (serialising them) by concatenating the strings together + * converts Values from plain text strings (deserialiasing them). + * Used to initialise the editor by deserializing "" into a Value. Apparently this is the idiomatic way to initialise a blank editor. + * Used (as a bodge) to turn a rich text editor into a MD editor, when deserialising the converted MD string of the editor into a value + + * PlainWithPillsSerializer + * A fork of slate-plain-serializer which is aware of Pills (hence the name) and Emoji. + * It can be configured to output Pills as: + * "plain": Pills are rendered via their 'completion' text - e.g. 'Matthew'; used for sending messages) + * "md": Pills are rendered as MD, e.g. [Matthew](https://matrix.to/#/@matthew:matrix.org) ) + * "id": Pills are rendered as IDs, e.g. '@matthew:matrix.org' (used for authoring / commands) + * Emoji nodes are converted to inline utf8 emoji. + +The actual conversion transitions are: + + * Quoting: + * The message being quoted is taken as HTML + * ...and deserialised into a Value + * ...and then serialised into MD via slate-md-serializer if the editor is in MD mode + + * Roundtripping between MD and rich text editor mode + * From MD to richtext (mdToRichEditorState): + * Serialise the MD-format Value to a MD string (converting pills to MD) with PlainWithPillsSerializer in 'md' mode + * Convert that MD string to HTML via Markdown.js + * Deserialise that Value to HTML via slate-html-serializer + * From richtext to MD (richToMdEditorState): + * Serialise the richtext-format Value to a MD string with slate-md-serializer (XXX: this should use commonmark) + * Deserialise that to a plain text value via slate-plain-serializer + + * Loading history in one format into an editor which is in the other format + * Uses the same functions as for roundtripping + + * Scanning the editor for a slash command + * If the editor is a single line node starting with /, then serialize it to a string with PlainWithPillsSerializer in 'id' mode + So that pills get converted to IDs suitable for commands being passed around + + * Sending messages + * In RT mode: + * If there is rich content, serialize the RT-format Value to HTML body via slate-html-serializer + * Serialize the RT-format Value to the plain text fallback via PlainWithPillsSerializer in 'plain' mode + * In MD mode: + * Serialize the MD-format Value into an MD string with PlainWithPillsSerializer in 'md' mode + * Parse the string with Markdown.js + * If it contains no formatting: + * Send as plaintext (as taken from Markdown.toPlainText()) + * Otherwise + * Send as HTML (as taken from Markdown.toHtml()) + * Serialize the RT-format Value to the plain text fallback via PlainWithPillsSerializer in 'plain' mode + + * Pasting HTML + * Deserialize HTML to a RT Value via slate-html-serializer + * In RT mode, insert it straight into the editor as a fragment + * In MD mode, serialise it to an MD string via slate-md-serializer and then insert the string into the editor as a fragment. + +The various scenarios and transitions could be drawn into a pretty diagram if one felt the urge, but hopefully the enough +gives sufficient detail on how it's all meant to work. \ No newline at end of file From 021409aafe176d9783e5234f649e8f0414f6cd43 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 9 Jul 2018 00:52:27 +0100 Subject: [PATCH 088/123] apply review feedback from @lukebarnard1 (cherry picked from commit 37d4bce) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/HtmlUtils.js | 36 ------- src/Markdown.js | 8 -- src/autocomplete/Autocompleter.js | 6 +- src/autocomplete/UserProvider.js | 2 +- .../views/rooms/MessageComposerInput.js | 94 ++++++------------- .../views/rooms/MessageComposerInput-test.js | 8 +- 6 files changed, 38 insertions(+), 116 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index ccfecb8081..09ce1187d5 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -112,42 +112,6 @@ export function charactersToImageNode(alt, useSvg, ...unicode) { />; } -/* -export function processHtmlForSending(html: string): string { - const contentDiv = document.createElement('div'); - contentDiv.innerHTML = html; - - if (contentDiv.children.length === 0) { - return contentDiv.innerHTML; - } - - let contentHTML = ""; - for (let i=0; i < contentDiv.children.length; i++) { - const element = contentDiv.children[i]; - if (element.tagName.toLowerCase() === 'p') { - contentHTML += element.innerHTML; - // Don't add a
    for the last

    - if (i !== contentDiv.children.length - 1) { - contentHTML += '
    '; - } - } else if (element.tagName.toLowerCase() === 'pre') { - // Replace "
    \n" with "\n" within `

    ` tags because the 
    is - // redundant. This is a workaround for a bug in draft-js-export-html: - // https://github.com/sstur/draft-js-export-html/issues/62 - contentHTML += '
    ' +
    -                element.innerHTML.replace(/
    \n/g, '\n').trim() + - '
    '; - } else { - const temp = document.createElement('div'); - temp.appendChild(element.cloneNode(true)); - contentHTML += temp.innerHTML; - } - } - - return contentHTML; -} -*/ - /* * Given an untrusted HTML string, return a React node with an sanitized version * of that HTML. diff --git a/src/Markdown.js b/src/Markdown.js index 9d9a8621c9..dc0d5962fd 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -180,14 +180,6 @@ export default class Markdown { if (is_multi_line(node) && node.next) this.lit('\n\n'); }; - // convert MD links into console-friendly ' < http://foo >' style links - // ...except given this function never gets called with links, it's useless. - // renderer.link = function(node, entering) { - // if (!entering) { - // this.lit(` < ${node.destination} >`); - // } - // }; - return renderer.render(this.parsed); } } diff --git a/src/autocomplete/Autocompleter.js b/src/autocomplete/Autocompleter.js index b3c20a713c..7f91676cc3 100644 --- a/src/autocomplete/Autocompleter.js +++ b/src/autocomplete/Autocompleter.js @@ -29,9 +29,9 @@ import NotifProvider from './NotifProvider'; import Promise from 'bluebird'; export type SelectionRange = { - beginning: boolean, - start: number, - end: number + beginning: boolean, // whether the selection is in the first block of the editor or not + start: number, // byte offset relative to the start anchor of the current editor selection. + end: number, // byte offset relative to the end anchor of the current editor selection. }; export type Completion = { diff --git a/src/autocomplete/UserProvider.js b/src/autocomplete/UserProvider.js index 156aac2eb8..7998337e2e 100644 --- a/src/autocomplete/UserProvider.js +++ b/src/autocomplete/UserProvider.js @@ -111,7 +111,7 @@ export default class UserProvider extends AutocompleteProvider { // relies on the length of the entity === length of the text in the decoration. completion: user.rawDisplayName.replace(' (IRC)', ''), completionId: user.userId, - suffix: (selection.beginning && range.start === 0) ? ': ' : ' ', + suffix: (selection.beginning && selection.start === 0) ? ': ' : ' ', href: makeUserPermalink(user.userId), component: ( { if (obj.object !== 'inline') return; switch (obj.type) { @@ -288,9 +296,6 @@ export default class MessageComposerInput extends React.Component { } ] }); - - this.suppressAutoComplete = false; - this.direction = ''; } /* @@ -298,7 +303,8 @@ export default class MessageComposerInput extends React.Component { * - whether we've got rich text mode enabled * - contentState was passed in */ - createEditorState(richText: boolean, editorState: ?Value): Value { + createEditorState(richText: boolean, // eslint-disable-line no-unused-vars + editorState: ?Value): Value { if (editorState instanceof Value) { return editorState; } @@ -371,7 +377,6 @@ export default class MessageComposerInput extends React.Component { let fragmentChange = fragment.change(); fragmentChange.moveToRangeOf(fragment.document) .wrapBlock(quote); - //.setBlocks('block-quote'); // FIXME: handle pills and use commonmark rather than md-serialize const md = this.md.serialize(fragmentChange.value); @@ -459,6 +464,7 @@ export default class MessageComposerInput extends React.Component { if (this.direction !== '') { const focusedNode = editorState.focusInline || editorState.focusText; if (focusedNode.isVoid) { + // XXX: does this work in RTL? const edge = this.direction === 'Previous' ? 'End' : 'Start'; if (editorState.isCollapsed) { change = change[`collapseTo${ edge }Of${ this.direction }Text`](); @@ -478,13 +484,6 @@ export default class MessageComposerInput extends React.Component { this.onFinishedTyping(); } - /* - // XXX: what was this ever doing? - if (!state.hasOwnProperty('originalEditorState')) { - state.originalEditorState = null; - } - */ - if (editorState.startText !== null) { const text = editorState.startText.text; const currentStartOffset = editorState.startOffset; @@ -512,9 +511,7 @@ export default class MessageComposerInput extends React.Component { } // emojioneify any emoji - - // XXX: is getTextsAsArray a private API? - editorState.document.getTextsAsArray().forEach(node => { + editorState.document.getTexts().forEach(node => { if (node.text !== '' && HtmlUtils.containsEmoji(node.text)) { let match; while ((match = EMOJI_REGEX.exec(node.text)) !== null) { @@ -546,36 +543,6 @@ export default class MessageComposerInput extends React.Component { editorState = change.value; } -/* - const currentBlock = editorState.getSelection().getStartKey(); - const currentSelection = editorState.getSelection(); - const currentStartOffset = editorState.getSelection().getStartOffset(); - - const block = editorState.getCurrentContent().getBlockForKey(currentBlock); - const text = block.getText(); - - const entityBeforeCurrentOffset = block.getEntityAt(currentStartOffset - 1); - const entityAtCurrentOffset = block.getEntityAt(currentStartOffset); - - // If the cursor is on the boundary between an entity and a non-entity and the - // text before the cursor has whitespace at the end, set the entity state of the - // character before the cursor (the whitespace) to null. This allows the user to - // stop editing the link. - if (entityBeforeCurrentOffset && !entityAtCurrentOffset && - /\s$/.test(text.slice(0, currentStartOffset))) { - editorState = RichUtils.toggleLink( - editorState, - currentSelection.merge({ - anchorOffset: currentStartOffset - 1, - focusOffset: currentStartOffset, - }), - null, - ); - // Reset selection - editorState = EditorState.forceSelection(editorState, currentSelection); - } -*/ - if (this.props.onInputStateChanged && editorState.blocks.size > 0) { let blockType = editorState.blocks.first().type; // console.log("onInputStateChanged; current block type is " + blockType + " and marks are " + editorState.activeMarks); @@ -605,7 +572,6 @@ export default class MessageComposerInput extends React.Component { editor_state: editorState, }); - /* Since a modification was made, set originalEditorState to null, since newState is now our original */ this.setState({ editorState, originalEditorState: originalEditorState || null @@ -683,7 +649,7 @@ export default class MessageComposerInput extends React.Component { hasMark = type => { const { editorState } = this.state - return editorState.activeMarks.some(mark => mark.type == type) + return editorState.activeMarks.some(mark => mark.type === type) }; /** @@ -695,7 +661,7 @@ export default class MessageComposerInput extends React.Component { hasBlock = type => { const { editorState } = this.state - return editorState.blocks.some(node => node.type == type) + return editorState.blocks.some(node => node.type === type) }; onKeyDown = (ev: KeyboardEvent, change: Change, editor: Editor) => { @@ -828,7 +794,7 @@ export default class MessageComposerInput extends React.Component { // Handle the extra wrapping required for list buttons. const isList = this.hasBlock('list-item'); const isType = editorState.blocks.some(block => { - return !!document.getClosest(block.key, parent => parent.type == type); + return !!document.getClosest(block.key, parent => parent.type === type); }); if (isList && isType) { @@ -839,7 +805,7 @@ export default class MessageComposerInput extends React.Component { } else if (isList) { change .unwrapBlock( - type == 'bulleted-list' ? 'numbered-list' : 'bulleted-list' + type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list' ) .wrapBlock(type); } else { @@ -1009,7 +975,7 @@ export default class MessageComposerInput extends React.Component { let contentHTML; // only look for commands if the first block contains simple unformatted text - // i.e. no pills or rich-text formatting. + // i.e. no pills or rich-text formatting and begins with a /. let cmd, commandText; const firstChild = editorState.document.nodes.get(0); const firstGrandChild = firstChild && firstChild.nodes.get(0); @@ -1090,8 +1056,8 @@ export default class MessageComposerInput extends React.Component { // didn't contain any formatting in the first place... contentText = mdWithPills.toPlaintext(); } else { - // to avoid ugliness clients which can't parse HTML we don't send pills - // in the plaintext body. + // to avoid ugliness on clients which ignore the HTML body we don't + // send pills in the plaintext body. contentText = this.plainWithPlainPills.serialize(editorState); contentHTML = mdWithPills.toHTML(); } @@ -1255,11 +1221,8 @@ export default class MessageComposerInput extends React.Component { // Move selection to the end of the selected history const change = editorState.change().collapseToEndOf(editorState.document); - // XXX: should we be calling this.onChange(change) now? - // Answer: yes, if we want it to do any of the fixups on stuff like emoji. - // however, this should already have been done and persisted in the history, - // so shouldn't be necessary. - + // We don't call this.onChange(change) now, as fixups on stuff like emoji + // should already have been done and persisted in the history. editorState = change.value; this.suppressAutoComplete = true; @@ -1362,6 +1325,8 @@ export default class MessageComposerInput extends React.Component { .insertText(suffix) .focus(); } + // for good hygiene, keep editorState updated to track the result of the change + // even though we don't do anything subsequently with it editorState = change.value; this.onChange(change, activeEditorState); @@ -1460,10 +1425,11 @@ export default class MessageComposerInput extends React.Component { }; onFormatButtonClicked = (name, e) => { - e.preventDefault(); // don't steal focus from the editor! + e.preventDefault(); // XXX: horrible evil hack to ensure the editor is focused so the act // of focusing it doesn't then cancel the format button being pressed + // FIXME: can we just tell handleKeyCommand's change to invoke .focus()? if (document.activeElement && document.activeElement.className !== 'mx_MessageComposer_editor') { this.refs.editor.focus(); setTimeout(()=>{ diff --git a/test/components/views/rooms/MessageComposerInput-test.js b/test/components/views/rooms/MessageComposerInput-test.js index 708071df23..662fbc7104 100644 --- a/test/components/views/rooms/MessageComposerInput-test.js +++ b/test/components/views/rooms/MessageComposerInput-test.js @@ -10,7 +10,6 @@ const MessageComposerInput = sdk.getComponent('views.rooms.MessageComposerInput' import MatrixClientPeg from '../../../../src/MatrixClientPeg'; import RoomMember from 'matrix-js-sdk'; -/* function addTextToDraft(text) { const components = document.getElementsByClassName('public-DraftEditor-content'); if (components && components.length) { @@ -21,7 +20,9 @@ function addTextToDraft(text) { } } -describe('MessageComposerInput', () => { +// FIXME: These tests need to be updated from Draft to Slate. + +xdescribe('MessageComposerInput', () => { let parentDiv = null, sandbox = null, client = null, @@ -300,5 +301,4 @@ describe('MessageComposerInput', () => { expect(spy.args[0][1].body).toEqual('[Click here](https://some.lovely.url)'); expect(spy.args[0][1].formatted_body).toEqual('Click here'); }); -}); -*/ \ No newline at end of file +}); \ No newline at end of file From 0d0934add7d83d7b1eac6ca5f92c9b985eee58c9 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 9 Jul 2018 00:58:35 +0100 Subject: [PATCH 089/123] unbreak modifier+space (e.g. emoji insert on macOS) (cherry picked from commit c490f87) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 77cc3ca2de..24834d01d7 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -713,6 +713,10 @@ export default class MessageComposerInput extends React.Component { }; onSpace = (ev: KeyboardEvent, change: Change): Change => { + if (ev.metaKey || ev.altKey || ev.shiftKey || ev.ctrlKey) { + return; + } + // drop a point in history so the user can undo a word // XXX: this seems nasty but adding to history manually seems a no-go ev.preventDefault(); From 8bcb987f507e485d84ec768d596a920d40fb6c6d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 9 Jul 2018 20:14:37 +0100 Subject: [PATCH 090/123] delint Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/autocomplete/CommandProvider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autocomplete/CommandProvider.js b/src/autocomplete/CommandProvider.js index 0377ff3395..850d97ab71 100644 --- a/src/autocomplete/CommandProvider.js +++ b/src/autocomplete/CommandProvider.js @@ -43,7 +43,7 @@ export default class CommandProvider extends AutocompleteProvider { let matches = []; // check if the full match differs from the first word (i.e. returns false if the command has args) - if (command[0] !== command[1]) { + if (command[0] !== command[1]) { // The input looks like a command with arguments, perform exact match const name = command[1].substr(1); // strip leading `/` if (CommandMap[name]) { From 51591a4d62ac056d3d730a212e6cb2bf666b1ed7 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 9 Jul 2018 20:11:17 +0100 Subject: [PATCH 091/123] fix lint --- src/components/structures/ContextualMenu.js | 3 ++- src/components/views/rooms/EventTile.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/structures/ContextualMenu.js b/src/components/structures/ContextualMenu.js index adc8dfd11c..7295fd45d3 100644 --- a/src/components/structures/ContextualMenu.js +++ b/src/components/structures/ContextualMenu.js @@ -220,7 +220,8 @@ export default class ContextualMenu extends React.Component { { chevron }
    - { props.hasBackground &&
    } + { props.hasBackground &&
    }
    ; } diff --git a/src/components/views/rooms/EventTile.js b/src/components/views/rooms/EventTile.js index 9ed73c39b1..fff04d476d 100644 --- a/src/components/views/rooms/EventTile.js +++ b/src/components/views/rooms/EventTile.js @@ -56,7 +56,7 @@ const stateEventTileTypes = { 'm.room.topic': 'messages.TextualEvent', 'm.room.power_levels': 'messages.TextualEvent', 'm.room.pinned_events': 'messages.TextualEvent', - 'm.room.server_acl' : 'messages.TextualEvent', + 'm.room.server_acl': 'messages.TextualEvent', 'im.vector.modular.widgets': 'messages.TextualEvent', }; From 58301e5dd455e4f992a1a4feb107f1224f155329 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 10 Jul 2018 10:28:17 +0100 Subject: [PATCH 092/123] navigateHistory only when at edges of document, to prevent Firefox bug Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 39 ++++--------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 24834d01d7..0f0d8787d6 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1140,41 +1140,18 @@ export default class MessageComposerInput extends React.Component { // Select history only if we are not currently auto-completing if (this.autocomplete.state.completionList.length === 0) { + const selection = this.state.editorState.selection; - // determine whether our cursor is at the top or bottom of the multiline - // input box by just looking at the position of the plain old DOM selection. - const selection = window.getSelection(); - const range = selection.getRangeAt(0); - const cursorRect = range.getBoundingClientRect(); + // selection must be collapsed + if (!selection.isCollapsed) return; + const document = this.state.editorState.document; - const editorNode = ReactDOM.findDOMNode(this.refs.editor); - const editorRect = editorNode.getBoundingClientRect(); - - // heuristic to handle tall emoji, pills, etc pushing the cursor away from the top - // or bottom of the page. - // XXX: is this going to break on large inline images or top-to-bottom scripts? - const EDGE_THRESHOLD = 15; - - let navigateHistory = false; + // and we must be at the edge of the document (up=start, down=end) if (up) { - const scrollCorrection = editorNode.scrollTop; - const distanceFromTop = cursorRect.top - editorRect.top + scrollCorrection; - console.log(`Cursor distance from editor top is ${distanceFromTop}`); - if (distanceFromTop < EDGE_THRESHOLD) { - navigateHistory = true; - } + if (!selection.isAtStartOf(document)) return; + } else { + if (!selection.isAtEndOf(document)) return; } - else { - const scrollCorrection = - editorNode.scrollHeight - editorNode.clientHeight - editorNode.scrollTop; - const distanceFromBottom = editorRect.bottom - cursorRect.bottom + scrollCorrection; - console.log(`Cursor distance from editor bottom is ${distanceFromBottom}`); - if (distanceFromBottom < EDGE_THRESHOLD) { - navigateHistory = true; - } - } - - if (!navigateHistory) return; const selected = this.selectHistory(up); if (selected) { From 100ecfe7cef57fda55d38e01d7c86160b09b608b Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 10 Jul 2018 10:29:52 +0100 Subject: [PATCH 093/123] remove trailing spaces to make linter happy (no-trailing-spaces) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 0f0d8787d6..fe2cf585ea 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -183,7 +183,7 @@ export default class MessageComposerInput extends React.Component { this.md = new Md({ rules: [ { - // if serialize returns undefined it falls through to the default hardcoded + // if serialize returns undefined it falls through to the default hardcoded // serialization rules serialize: (obj, children) => { if (obj.object !== 'inline') return; @@ -304,7 +304,7 @@ export default class MessageComposerInput extends React.Component { * - contentState was passed in */ createEditorState(richText: boolean, // eslint-disable-line no-unused-vars - editorState: ?Value): Value { + editorState: ?Value): Value { if (editorState instanceof Value) { return editorState; } @@ -359,7 +359,7 @@ export default class MessageComposerInput extends React.Component { // If so, what should be the format, and how do we differentiate it from replies? const quote = Block.create('block-quote'); - if (this.state.isRichTextEnabled) { + if (this.state.isRichTextEnabled) { let change = editorState.change(); if (editorState.anchorText.text === '' && editorState.anchorBlock.nodes.size === 1) { // replace the current block rather than split the block From abbb69dc3666997ec138a77677c052beb200be5e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 10 Jul 2018 17:35:13 +0100 Subject: [PATCH 094/123] fix fn call, fixes usage of SlashCommands Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index fe2cf585ea..e7fbbae8a8 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -988,7 +988,7 @@ export default class MessageComposerInput extends React.Component { firstGrandChild.text[0] === '/') { commandText = this.plainWithIdPills.serialize(editorState); - cmd = SlashCommands.processInput(this.props.room.roomId, commandText); + cmd = processCommandInput(this.props.room.roomId, commandText); } if (cmd) { From fd4f9679df7b035c0112dce135c47967713f9518 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 11 Jul 2018 09:39:14 +0100 Subject: [PATCH 095/123] convert md<->rt if the stored editorState was in a different state Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 58 +++++++++++-------- src/stores/MessageComposerStore.js | 5 +- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index e7fbbae8a8..c8d8c9d2f9 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -143,27 +143,6 @@ export default class MessageComposerInput extends React.Component { Analytics.setRichtextMode(isRichTextEnabled); - this.state = { - // whether we're in rich text or markdown mode - isRichTextEnabled, - - // the currently displayed editor state (note: this is always what is modified on input) - editorState: this.createEditorState( - isRichTextEnabled, - MessageComposerStore.getEditorState(this.props.room.roomId), - ), - - // the original editor state, before we started tabbing through completions - originalEditorState: null, - - // the virtual state "above" the history stack, the message currently being composed that - // we want to persist whilst browsing history - currentlyComposedEditorState: null, - - // whether there were any completions - someCompletions: null, - }; - this.client = MatrixClientPeg.get(); // track whether we should be trying to show autocomplete suggestions on the current editor @@ -296,19 +275,47 @@ export default class MessageComposerInput extends React.Component { } ] }); + + const savedState = MessageComposerStore.getEditorState(this.props.room.roomId); + this.state = { + // whether we're in rich text or markdown mode + isRichTextEnabled, + + // the currently displayed editor state (note: this is always what is modified on input) + editorState: this.createEditorState( + isRichTextEnabled, + savedState.editor_state, + savedState.rich_text, + ), + + // the original editor state, before we started tabbing through completions + originalEditorState: null, + + // the virtual state "above" the history stack, the message currently being composed that + // we want to persist whilst browsing history + currentlyComposedEditorState: null, + + // whether there were any completions + someCompletions: null, + }; } /* * "Does the right thing" to create an Editor value, based on: * - whether we've got rich text mode enabled * - contentState was passed in + * - whether the contentState that was passed in was rich text */ - createEditorState(richText: boolean, // eslint-disable-line no-unused-vars - editorState: ?Value): Value { + createEditorState(wantRichText: boolean, editorState: ?Value, wasRichText: ?boolean): Value { if (editorState instanceof Value) { + if (wantRichText && !wasRichText) { + return this.mdToRichEditorState(editorState); + } + if (wasRichText && !wantRichText) { + return this.richToMdEditorState(editorState); + } return editorState; - } - else { + } else { // ...or create a new one. return Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); } @@ -569,6 +576,7 @@ export default class MessageComposerInput extends React.Component { dis.dispatch({ action: 'editor_state', room_id: this.props.room.roomId, + rich_text: this.state.isRichTextEnabled, editor_state: editorState, }); diff --git a/src/stores/MessageComposerStore.js b/src/stores/MessageComposerStore.js index 0e6c856e1b..e70714ff98 100644 --- a/src/stores/MessageComposerStore.js +++ b/src/stores/MessageComposerStore.js @@ -54,7 +54,10 @@ class MessageComposerStore extends Store { _editorState(payload) { const editorStateMap = this._state.editorStateMap; - editorStateMap[payload.room_id] = payload.editor_state; + editorStateMap[payload.room_id] = { + editor_state: payload.editor_state, + rich_text: payload.rich_text, + }; localStorage.setItem('editor_state', JSON.stringify(editorStateMap)); this._setState({ editorStateMap: editorStateMap, From c3aef6e3a0d153eb38eb5f1b8ac864896b753367 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 11 Jul 2018 10:29:14 +0100 Subject: [PATCH 096/123] workaround for tommoor/slate-md-serializer#14 Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index c8d8c9d2f9..d0079309e3 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -182,6 +182,11 @@ export default class MessageComposerInput extends React.Component { return `${ children }`; case 'deleted': return `${ children }`; + case 'code': + // XXX: we only ever get given `code` regardless of whether it was inline or block + // XXX: workaround for https://github.com/tommoor/slate-md-serializer/issues/14 + // strip single backslashes from children, as they would have been escaped here + return `\`${ children.split('\\').map((v) => v ? v : '\\').join('') }\``; } }, }, From 95909de446f141a01e3eeca56906e8ce69f22b7e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 11 Jul 2018 11:39:55 +0100 Subject: [PATCH 097/123] fix MessageComposer not marking translatable strings. run gen-i18n Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposer.js | 36 ++++++++++++------- src/i18n/strings/en_EN.json | 17 +++++---- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 746f3909f2..657fd463fd 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -16,7 +16,7 @@ limitations under the License. */ import React from 'react'; import PropTypes from 'prop-types'; -import { _t } from '../../../languageHandler'; +import { _t, _td } from '../../../languageHandler'; import CallHandler from '../../../CallHandler'; import MatrixClientPeg from '../../../MatrixClientPeg'; import Modal from '../../../Modal'; @@ -26,6 +26,17 @@ import RoomViewStore from '../../../stores/RoomViewStore'; import SettingsStore, {SettingLevel} from "../../../settings/SettingsStore"; import Stickerpicker from './Stickerpicker'; +const formatButtonList = [ + _td("bold"), + _td("italic"), + _td("deleted"), + _td("underlined"), + _td("inline-code"), + _td("block-quote"), + _td("bulleted-list"), + _td("numbered-list"), +]; + export default class MessageComposer extends React.Component { constructor(props, context) { super(props, context); @@ -322,18 +333,17 @@ export default class MessageComposer extends React.Component { let formatBar; if (this.state.showFormatting && this.state.inputState.isRichTextEnabled) { const {marks, blockType} = this.state.inputState; - const formatButtons = ["bold", "italic", "deleted", "underlined", "inline-code", "block-quote", "bulleted-list", "numbered-list"].map( - (name) => { - const active = marks.some(mark => mark.type === name) || blockType === name; - const suffix = active ? '-on' : ''; - const onFormatButtonClicked = this.onFormatButtonClicked.bind(this, name); - const className = 'mx_MessageComposer_format_button mx_filterFlipColor'; - return ; + const formatButtons = formatButtonList.map((name) => { + const active = marks.some(mark => mark.type === name) || blockType === name; + const suffix = active ? '-on' : ''; + const onFormatButtonClicked = this.onFormatButtonClicked.bind(this, name); + const className = 'mx_MessageComposer_format_button mx_filterFlipColor'; + return ; }, ); diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 486ecdf114..2da820d751 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -406,6 +406,14 @@ "Invited": "Invited", "Filter room members": "Filter room members", "%(userName)s (power %(powerLevelNumber)s)": "%(userName)s (power %(powerLevelNumber)s)", + "bold": "bold", + "italic": "italic", + "deleted": "deleted", + "underlined": "underlined", + "inline-code": "inline-code", + "block-quote": "block-quote", + "bulleted-list": "bulleted-list", + "numbered-list": "numbered-list", "Attachment": "Attachment", "At this time it is not possible to reply with a file so this will be sent without being a reply.": "At this time it is not possible to reply with a file so this will be sent without being a reply.", "Upload Files": "Upload Files", @@ -430,14 +438,6 @@ "Command error": "Command error", "Unable to reply": "Unable to reply", "At this time it is not possible to reply with an emote.": "At this time it is not possible to reply with an emote.", - "bold": "bold", - "italic": "italic", - "strike": "strike", - "underline": "underline", - "code": "code", - "quote": "quote", - "bullet": "bullet", - "numbullet": "numbullet", "Markdown is disabled": "Markdown is disabled", "Markdown is enabled": "Markdown is enabled", "No pinned messages.": "No pinned messages.", @@ -772,7 +772,6 @@ "Room directory": "Room directory", "Start chat": "Start chat", "And %(count)s more...|other": "And %(count)s more...", - "Share Link to User": "Share Link to User", "ex. @bob:example.com": "ex. @bob:example.com", "Add User": "Add User", "Matrix ID": "Matrix ID", From 3e05bf19c59a401efe53f39eed7be38aafb3b41f Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 11 Jul 2018 16:30:45 +0100 Subject: [PATCH 098/123] hide autocomplete when moving caret to match existing behaviour Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index d0079309e3..7b9fb89404 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -115,6 +115,15 @@ function onSendMessageFailed(err, room) { }); } +function rangeEquals(a: Range, b: Range): boolean { + return (a.anchorKey === b.anchorKey + && a.anchorOffset === b.anchorOffset + && a.focusKey === b.focusKey + && a.focusOffset === b.focusOffset + && a.isFocused === b.isFocused + && a.isBackward === b.isBackward); +} + /* * The textInput part of the MessageComposer */ @@ -469,8 +478,7 @@ export default class MessageComposerInput extends React.Component { } } - onChange = (change: Change, originalEditorState: value) => { - + onChange = (change: Change, originalEditorState?: Value) => { let editorState = change.value; if (this.direction !== '') { @@ -490,6 +498,11 @@ export default class MessageComposerInput extends React.Component { } } + // when selection changes hide the autocomplete + if (!rangeEquals(this.state.editorState.selection, editorState.selection)) { + this.autocomplete.hide(); + } + if (!editorState.document.isEmpty) { this.onTypingActivity(); } else { From b4bc09c3350dac7bdb867e0acb89e406ac6c9253 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 11 Jul 2018 17:13:33 +0100 Subject: [PATCH 099/123] null-guard savedState since now we're accessing its props Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 7b9fb89404..f9395abe59 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -298,8 +298,8 @@ export default class MessageComposerInput extends React.Component { // the currently displayed editor state (note: this is always what is modified on input) editorState: this.createEditorState( isRichTextEnabled, - savedState.editor_state, - savedState.rich_text, + savedState ? savedState.editor_state : undefined, + savedState ? savedState.rich_text : undefined, ), // the original editor state, before we started tabbing through completions From b412b9e84d8023d2c4ba5cfac4a28f87b814c74c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 12 Jul 2018 15:19:25 +0200 Subject: [PATCH 100/123] Fix stopPeeking not being called at the right time stopPeeking is currently not called when navigating to a joined room after having peeked a room. This causes the /events endpoint for the peeked room to be called until peeking another room, even when not viewing the peeked room anymore. The current code would only stop peeking if you joining were true (note the nesting), e.g. when waiting for your join to be confirmed by /sync. This change might make stopPeeking called also when not needed by there is a guard in that method to do nothing if not currently peeking. --- src/components/structures/RoomView.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/structures/RoomView.js b/src/components/structures/RoomView.js index 98d700e0a0..b9d0843635 100644 --- a/src/components/structures/RoomView.js +++ b/src/components/structures/RoomView.js @@ -299,11 +299,11 @@ module.exports = React.createClass({ throw err; } }); + } else if (room) { + // Stop peeking because we have joined this room previously + MatrixClientPeg.get().stopPeeking(); + this.setState({isPeeking: false}); } - } else if (room) { - // Stop peeking because we have joined this room previously - MatrixClientPeg.get().stopPeeking(); - this.setState({isPeeking: false}); } }, From 7405c5eff2f05d4a53633e936f515d70acc02924 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 12 Jul 2018 16:35:42 +0100 Subject: [PATCH 101/123] specify alternate history storage key to prevent conflicts with draft Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index f9395abe59..89e4107a56 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -337,7 +337,7 @@ export default class MessageComposerInput extends React.Component { componentDidMount() { this.dispatcherRef = dis.register(this.onAction); - this.historyManager = new ComposerHistoryManager(this.props.room.roomId); + this.historyManager = new ComposerHistoryManager(this.props.room.roomId, 'mx_slate_composer_history_'); } componentWillUnmount() { From e56feea9ec235c4d00afc82389a47ec3559c875f Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 12 Jul 2018 18:43:49 +0100 Subject: [PATCH 102/123] Put always-on-screen widgets in top left always-on-screen widgets now appear in the top-left where the call preview normally is if you're not in the room that they're in. Fixes https://github.com/vector-im/riot-web/issues/7007 Based off https://github.com/matrix-org/matrix-react-sdk/pull/2053 --- package.json | 1 + res/css/structures/_LeftPanel.scss | 4 + res/css/views/rooms/_AppsDrawer.scss | 6 ++ src/components/views/elements/AppTile.js | 15 +++- .../views/elements/PersistedElement.js | 22 ++++- .../views/elements/PersistentApp.js | 88 +++++++++++++++++++ src/components/views/rooms/AppsDrawer.js | 60 +------------ src/components/views/voip/CallPreview.js | 5 +- src/stores/ActiveWidgetStore.js | 19 ++++ src/utils/WidgetUtils.js | 64 ++++++++++++++ 10 files changed, 217 insertions(+), 67 deletions(-) create mode 100644 src/components/views/elements/PersistentApp.js diff --git a/package.json b/package.json index 8c0ec922f2..570f57abb5 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "react-beautiful-dnd": "^4.0.1", "react-dom": "^15.6.0", "react-gemini-scrollbar": "matrix-org/react-gemini-scrollbar#5e97aef", + "resize-observer-polyfill": "^1.5.0", "sanitize-html": "^1.14.1", "text-encoding-utf-8": "^1.0.1", "url": "^0.11.0", diff --git a/res/css/structures/_LeftPanel.scss b/res/css/structures/_LeftPanel.scss index 96ed5878ac..bedec0e363 100644 --- a/res/css/structures/_LeftPanel.scss +++ b/res/css/structures/_LeftPanel.scss @@ -54,6 +54,10 @@ limitations under the License. } +.mx_LeftPanel .mx_AppTileFullWidth { + height: 132px; +} + .mx_LeftPanel .mx_RoomList_scrollbar { order: 1; diff --git a/res/css/views/rooms/_AppsDrawer.scss b/res/css/views/rooms/_AppsDrawer.scss index 28d432686d..6431853672 100644 --- a/res/css/views/rooms/_AppsDrawer.scss +++ b/res/css/views/rooms/_AppsDrawer.scss @@ -126,6 +126,12 @@ limitations under the License. overflow: hidden; } +.mx_AppTileBody_mini { + height: 132px; + width: 100%; + overflow: hidden; +} + .mx_AppTileBody iframe { width: 100%; height: 280px; diff --git a/src/components/views/elements/AppTile.js b/src/components/views/elements/AppTile.js index cf69727b15..e287abd07a 100644 --- a/src/components/views/elements/AppTile.js +++ b/src/components/views/elements/AppTile.js @@ -164,6 +164,7 @@ export default class AppTile extends React.Component { PersistedElement.destroyElement(this._persistKey); ActiveWidgetStore.delWidgetMessaging(this.props.id); ActiveWidgetStore.delWidgetCapabilities(this.props.id); + ActiveWidgetStore.delRoomId(this.props.id); } } @@ -343,6 +344,7 @@ export default class AppTile extends React.Component { if (!ActiveWidgetStore.getWidgetMessaging(this.props.id)) { this._setupWidgetMessaging(); } + ActiveWidgetStore.setRoomId(this.props.id, this.props.room.roomId); this.setState({loading: false}); } @@ -522,6 +524,8 @@ export default class AppTile extends React.Component { // (see - https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-permissions-in-cross-origin-iframes and https://wicg.github.io/feature-policy/) const iframeFeatures = "microphone; camera; encrypted-media;"; + const appTileBodyClass = 'mx_AppTileBody' + (this.props.miniMode ? '_mini ' : ' '); + if (this.props.show) { const loadingElement = (
    @@ -530,20 +534,20 @@ export default class AppTile extends React.Component { ); if (this.state.initialising) { appTileBody = ( -
    +
    { loadingElement }
    ); } else if (this.state.hasPermissionToLoad == true) { if (this.isMixedContent()) { appTileBody = ( -
    +
    ); } else { appTileBody = ( -
    +
    { this.state.loading && loadingElement } { /* The "is" attribute in the following iframe tag is needed in order to enable rendering of the @@ -573,7 +577,7 @@ export default class AppTile extends React.Component { } else { const isRoomEncrypted = MatrixClientPeg.get().isRoomEncrypted(this.props.room.roomId); appTileBody = ( -
    +
    { + return ev.getStateKey() === ActiveWidgetStore.getPersistentWidgetId(); + }); + const app = WidgetUtils.makeAppConfig( + appEvent.getStateKey(), appEvent.getContent(), appEvent.sender, persistentWidgetInRoomId, + ); + const capWhitelist = WidgetUtils.getCapWhitelistForAppTypeInRoomId(app.type, persistentWidgetInRoomId); + const AppTile = sdk.getComponent('elements.AppTile'); + return ; + } + } + return null; + }, +}); + diff --git a/src/components/views/rooms/AppsDrawer.js b/src/components/views/rooms/AppsDrawer.js index da8c558cb5..aa086f8260 100644 --- a/src/components/views/rooms/AppsDrawer.js +++ b/src/components/views/rooms/AppsDrawer.js @@ -107,55 +107,6 @@ module.exports = React.createClass({ } }, - /** - * Encodes a URI according to a set of template variables. Variables will be - * passed through encodeURIComponent. - * @param {string} pathTemplate The path with template variables e.g. '/foo/$bar'. - * @param {Object} variables The key/value pairs to replace the template - * variables with. E.g. { '$bar': 'baz' }. - * @return {string} The result of replacing all template variables e.g. '/foo/baz'. - */ - encodeUri: function(pathTemplate, variables) { - for (const key in variables) { - if (!variables.hasOwnProperty(key)) { - continue; - } - pathTemplate = pathTemplate.replace( - key, encodeURIComponent(variables[key]), - ); - } - return pathTemplate; - }, - - _initAppConfig: function(appId, app, sender) { - const user = MatrixClientPeg.get().getUser(this.props.userId); - const params = { - '$matrix_user_id': this.props.userId, - '$matrix_room_id': this.props.room.roomId, - '$matrix_display_name': user ? user.displayName : this.props.userId, - '$matrix_avatar_url': user ? MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl) : '', - - // TODO: Namespace themes through some standard - '$theme': SettingsStore.getValue("theme"), - }; - - app.id = appId; - app.name = app.name || app.type; - - if (app.data) { - Object.keys(app.data).forEach((key) => { - params['$' + key] = app.data[key]; - }); - - app.waitForIframeLoad = (app.data.waitForIframeLoad === 'false' ? false : true); - } - - app.url = this.encodeUri(app.url, params); - app.creatorUserId = (sender && sender.userId) ? sender.userId : null; - - return app; - }, - onRoomStateEvents: function(ev, state) { if (ev.getRoomId() !== this.props.room.roomId || ev.getType() !== 'im.vector.modular.widgets') { return; @@ -165,7 +116,7 @@ module.exports = React.createClass({ _getApps: function() { return WidgetUtils.getRoomWidgets(this.props.room).map((ev) => { - return this._initAppConfig(ev.getStateKey(), ev.getContent(), ev.sender); + return WidgetUtils.makeAppConfig(ev.getStateKey(), ev.getContent(), ev.sender, this.props.room.roomId); }); }, @@ -213,15 +164,8 @@ module.exports = React.createClass({ }, render: function() { - const enableScreenshots = SettingsStore.getValue("enableWidgetScreenshots", this.props.room.room_id); - const apps = this.state.apps.map((app, index, arr) => { - const capWhitelist = enableScreenshots ? ["m.capability.screenshot"] : []; - - // Obviously anyone that can add a widget can claim it's a jitsi widget, - // so this doesn't really offer much over the set of domains we load - // widgets from at all, but it probably makes sense for sanity. - if (app.type == 'jitsi') capWhitelist.push("m.always_on_screen"); + const capWhitelist = WidgetUtils.getCapWhitelistForAppTypeInRoomId(app.type, this.props.room.roomId); return ( ); } - return null; + const PersistentApp = sdk.getComponent('elements.PersistentApp'); + return ; }, }); diff --git a/src/stores/ActiveWidgetStore.js b/src/stores/ActiveWidgetStore.js index 7c179aef84..01d5f15601 100644 --- a/src/stores/ActiveWidgetStore.js +++ b/src/stores/ActiveWidgetStore.js @@ -32,6 +32,9 @@ class ActiveWidgetStore { // A WidgetMessaging instance for each widget ID this._widgetMessagingByWidgetId = {}; + + // What room ID each widget is associated with (if it's a room widget) + this._roomIdByWidgetId = {}; } setWidgetPersistence(widgetId, val) { @@ -46,6 +49,10 @@ class ActiveWidgetStore { return this._persistentWidgetId === widgetId; } + getPersistentWidgetId() { + return this._persistentWidgetId; + } + setWidgetCapabilities(widgetId, caps) { this._capsByWidgetId[widgetId] = caps; } @@ -76,6 +83,18 @@ class ActiveWidgetStore { delete this._widgetMessagingByWidgetId[widgetId]; } } + + getRoomId(widgetId) { + return this._roomIdByWidgetId[widgetId]; + } + + setRoomId(widgetId, roomId) { + this._roomIdByWidgetId[widgetId] = roomId; + } + + delRoomId(widgetId) { + delete this._roomIdByWidgetId[widgetId]; + } } if (global.singletonActiveWidgetStore === undefined) { diff --git a/src/utils/WidgetUtils.js b/src/utils/WidgetUtils.js index ab5b5b0130..98239b3cec 100644 --- a/src/utils/WidgetUtils.js +++ b/src/utils/WidgetUtils.js @@ -19,6 +19,27 @@ import MatrixClientPeg from '../MatrixClientPeg'; import SdkConfig from "../SdkConfig"; import dis from '../dispatcher'; import * as url from "url"; +import SettingsStore from "../settings/SettingsStore"; + +/** + * Encodes a URI according to a set of template variables. Variables will be + * passed through encodeURIComponent. + * @param {string} pathTemplate The path with template variables e.g. '/foo/$bar'. + * @param {Object} variables The key/value pairs to replace the template + * variables with. E.g. { '$bar': 'baz' }. + * @return {string} The result of replacing all template variables e.g. '/foo/baz'. + */ +function encodeUri(pathTemplate, variables) { + for (const key in variables) { + if (!variables.hasOwnProperty(key)) { + continue; + } + pathTemplate = pathTemplate.replace( + key, encodeURIComponent(variables[key]), + ); + } + return pathTemplate; +} export default class WidgetUtils { /* Returns true if user is able to send state events to modify widgets in this room @@ -324,4 +345,47 @@ export default class WidgetUtils { }); return client.setAccountData('m.widgets', userWidgets); } + + static makeAppConfig(appId, app, sender, roomId) { + const myUserId = MatrixClientPeg.get().credentials.userId; + const user = MatrixClientPeg.get().getUser(myUserId); + const params = { + '$matrix_user_id': myUserId, + '$matrix_room_id': roomId, + '$matrix_display_name': user ? user.displayName : myUserId, + '$matrix_avatar_url': user ? MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl) : '', + + // TODO: Namespace themes through some standard + '$theme': SettingsStore.getValue("theme"), + }; + + app.id = appId; + app.name = app.name || app.type; + + if (app.data) { + Object.keys(app.data).forEach((key) => { + params['$' + key] = app.data[key]; + }); + + app.waitForIframeLoad = (app.data.waitForIframeLoad === 'false' ? false : true); + } + + app.url = encodeUri(app.url, params); + app.creatorUserId = (sender && sender.userId) ? sender.userId : null; + + return app; + } + + static getCapWhitelistForAppTypeInRoomId(appType, roomId) { + const enableScreenshots = SettingsStore.getValue("enableWidgetScreenshots", roomId); + + const capWhitelist = enableScreenshots ? ["m.capability.screenshot"] : []; + + // Obviously anyone that can add a widget can claim it's a jitsi widget, + // so this doesn't really offer much over the set of domains we load + // widgets from at all, but it probably makes sense for sanity. + if (appType == 'jitsi') capWhitelist.push("m.always_on_screen"); + + return capWhitelist; + } } From 59a14f2c0b562eda6c75747551df1673aa1aa15c Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sun, 15 Jul 2018 20:28:41 +0100 Subject: [PATCH 103/123] re-hydrate Values which have been serialized into LocalStorage Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/stores/MessageComposerStore.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/stores/MessageComposerStore.js b/src/stores/MessageComposerStore.js index e70714ff98..1d37a7c9e5 100644 --- a/src/stores/MessageComposerStore.js +++ b/src/stores/MessageComposerStore.js @@ -1,5 +1,5 @@ /* -Copyright 2017 Vector Creations Ltd +Copyright 2017, 2018 Vector Creations Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ limitations under the License. */ import dis from '../dispatcher'; import { Store } from 'flux/utils'; +import { Value } from 'slate'; const INITIAL_STATE = { // a map of room_id to rich text editor composer state @@ -65,7 +66,15 @@ class MessageComposerStore extends Store { } getEditorState(roomId) { - return this._state.editorStateMap[roomId]; + const editorStateMap = this._state.editorStateMap; + // const entry = this._state.editorStateMap[roomId]; + if (editorStateMap[roomId] && !Value.isValue(editorStateMap[roomId].editor_state)) { + // rehydrate lazily to prevent massive churn at launch and cache it + editorStateMap[roomId].editor_state = Value.fromJSON(editorStateMap[roomId].editor_state); + } + // explicitly don't setState here because the value didn't actually change, we just hydrated it, + // if a listener received an update they too would call this method and have a hydrated Value + return editorStateMap[roomId]; } reset() { From ff96537048dd940884f6a904479a0887a409f4d1 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 16 Jul 2018 11:49:53 +0100 Subject: [PATCH 104/123] lint --- src/components/views/elements/PersistentApp.js | 1 - src/components/views/rooms/AppsDrawer.js | 1 - 2 files changed, 2 deletions(-) diff --git a/src/components/views/elements/PersistentApp.js b/src/components/views/elements/PersistentApp.js index 0f419ab874..bbb3b2a9c8 100644 --- a/src/components/views/elements/PersistentApp.js +++ b/src/components/views/elements/PersistentApp.js @@ -15,7 +15,6 @@ limitations under the License. */ import React from 'react'; -import PropTypes from 'prop-types'; import RoomViewStore from '../../../stores/RoomViewStore'; import ActiveWidgetStore from '../../../stores/ActiveWidgetStore'; import WidgetUtils from '../../../utils/WidgetUtils'; diff --git a/src/components/views/rooms/AppsDrawer.js b/src/components/views/rooms/AppsDrawer.js index aa086f8260..80f899f8e6 100644 --- a/src/components/views/rooms/AppsDrawer.js +++ b/src/components/views/rooms/AppsDrawer.js @@ -29,7 +29,6 @@ import ScalarAuthClient from '../../../ScalarAuthClient'; import ScalarMessaging from '../../../ScalarMessaging'; import { _t } from '../../../languageHandler'; import WidgetUtils from '../../../utils/WidgetUtils'; -import SettingsStore from "../../../settings/SettingsStore"; // The maximum number of widgets that can be added in a room const MAX_WIDGETS = 2; From d7ff7cd4ed1e00c695b811b17e5313b1c3d8cf77 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 16 Jul 2018 12:00:15 +0100 Subject: [PATCH 105/123] stupid thinkotypo --- docs/slate-formats.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/slate-formats.md b/docs/slate-formats.md index b526109c1b..7bb2fc9c5f 100644 --- a/docs/slate-formats.md +++ b/docs/slate-formats.md @@ -84,5 +84,5 @@ The actual conversion transitions are: * In RT mode, insert it straight into the editor as a fragment * In MD mode, serialise it to an MD string via slate-md-serializer and then insert the string into the editor as a fragment. -The various scenarios and transitions could be drawn into a pretty diagram if one felt the urge, but hopefully the enough +The various scenarios and transitions could be drawn into a pretty diagram if one felt the urge, but hopefully the above gives sufficient detail on how it's all meant to work. \ No newline at end of file From 19f0617052aa2788aced5f27931fffd1fc97a65e Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 16 Jul 2018 15:41:03 +0100 Subject: [PATCH 106/123] Jitsi: Error message on no permission Fixes https://github.com/vector-im/riot-web/issues/7007 --- src/CallHandler.js | 8 +++++ src/i18n/strings/en_EN.json | 66 +++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/CallHandler.js b/src/CallHandler.js index 7403483e36..5349e628bb 100644 --- a/src/CallHandler.js +++ b/src/CallHandler.js @@ -461,6 +461,14 @@ function _startCallApp(roomId, type) { WidgetUtils.setRoomWidget(roomId, widgetId, 'jitsi', widgetUrl, 'Jitsi', widgetData).then(() => { console.log('Jitsi widget added'); }).catch((e) => { + if (e.errcode === 'M_FORBIDDEN') { + const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); + + Modal.createTrackedDialog('Call Failed', '', ErrorDialog, { + title: _t('Permission Required'), + description: _t("You do not have permission to start a conference call in this room"), + }); + } console.error(e); }); } diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 360e076bd8..39eba74519 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -41,13 +41,11 @@ "Conference call failed.": "Conference call failed.", "Call in Progress": "Call in Progress", "A call is already in progress!": "A call is already in progress!", + "Permission Required": "Permission Required", + "You do not have permission to start a conference call in this room": "You do not have permission to start a conference call in this room", "The file '%(fileName)s' failed to upload": "The file '%(fileName)s' failed to upload", "The file '%(fileName)s' exceeds this home server's size limit for uploads": "The file '%(fileName)s' exceeds this home server's size limit for uploads", "Upload Failed": "Upload Failed", - "Failure to create room": "Failure to create room", - "Server may be unavailable, overloaded, or you hit a bug.": "Server may be unavailable, overloaded, or you hit a bug.", - "Send anyway": "Send anyway", - "Send": "Send", "Sun": "Sun", "Mon": "Mon", "Tue": "Tue", @@ -87,7 +85,6 @@ "Failed to invite users to community": "Failed to invite users to community", "Failed to invite users to %(groupId)s": "Failed to invite users to %(groupId)s", "Failed to add the following rooms to %(groupId)s:": "Failed to add the following rooms to %(groupId)s:", - "Unnamed Room": "Unnamed Room", "Riot does not have permission to send you notifications - please check your browser settings": "Riot does not have permission to send you notifications - please check your browser settings", "Riot was not given permission to send notifications - please try again": "Riot was not given permission to send notifications - please try again", "Unable to enable Notifications": "Unable to enable Notifications", @@ -201,6 +198,11 @@ "%(names)s and %(count)s others are typing|other": "%(names)s and %(count)s others are typing", "%(names)s and %(count)s others are typing|one": "%(names)s and one other is typing", "%(names)s and %(lastPerson)s are typing": "%(names)s and %(lastPerson)s are typing", + "Failure to create room": "Failure to create room", + "Server may be unavailable, overloaded, or you hit a bug.": "Server may be unavailable, overloaded, or you hit a bug.", + "Send anyway": "Send anyway", + "Send": "Send", + "Unnamed Room": "Unnamed Room", "Your browser does not support the required cryptography extensions": "Your browser does not support the required cryptography extensions", "Not a valid Riot keyfile": "Not a valid Riot keyfile", "Authentication check failed: incorrect password?": "Authentication check failed: incorrect password?", @@ -314,31 +316,6 @@ "Off": "Off", "On": "On", "Noisy": "Noisy", - "Invalid alias format": "Invalid alias format", - "'%(alias)s' is not a valid format for an alias": "'%(alias)s' is not a valid format for an alias", - "Invalid address format": "Invalid address format", - "'%(alias)s' is not a valid format for an address": "'%(alias)s' is not a valid format for an address", - "not specified": "not specified", - "not set": "not set", - "Remote addresses for this room:": "Remote addresses for this room:", - "Addresses": "Addresses", - "The main address for this room is": "The main address for this room is", - "Local addresses for this room:": "Local addresses for this room:", - "This room has no local addresses": "This room has no local addresses", - "New address (e.g. #foo:%(localDomain)s)": "New address (e.g. #foo:%(localDomain)s)", - "Invalid community ID": "Invalid community ID", - "'%(groupId)s' is not a valid community ID": "'%(groupId)s' is not a valid community ID", - "Flair": "Flair", - "Showing flair for these communities:": "Showing flair for these communities:", - "This room is not showing flair for any communities": "This room is not showing flair for any communities", - "New community ID (e.g. +foo:%(localDomain)s)": "New community ID (e.g. +foo:%(localDomain)s)", - "You have enabled URL previews by default.": "You have enabled URL previews by default.", - "You have disabled URL previews by default.": "You have disabled URL previews by default.", - "URL previews are enabled by default for participants in this room.": "URL previews are enabled by default for participants in this room.", - "URL previews are disabled by default for participants in this room.": "URL previews are disabled by default for participants in this room.", - "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.": "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.", - "URL Previews": "URL Previews", - "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.": "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.", "Cannot add any more widgets": "Cannot add any more widgets", "The maximum permitted number of widgets have already been added to this room.": "The maximum permitted number of widgets have already been added to this room.", "Add a widget": "Add a widget", @@ -441,11 +418,11 @@ "At this time it is not possible to reply with an emote.": "At this time it is not possible to reply with an emote.", "Markdown is disabled": "Markdown is disabled", "Markdown is enabled": "Markdown is enabled", + "Unpin Message": "Unpin Message", + "Jump to message": "Jump to message", "No pinned messages.": "No pinned messages.", "Loading...": "Loading...", "Pinned Messages": "Pinned Messages", - "Unpin Message": "Unpin Message", - "Jump to message": "Jump to message", "%(duration)ss": "%(duration)ss", "%(duration)sm": "%(duration)sm", "%(duration)sh": "%(duration)sh", @@ -579,6 +556,31 @@ "Scroll to unread messages": "Scroll to unread messages", "Jump to first unread message.": "Jump to first unread message.", "Close": "Close", + "Invalid alias format": "Invalid alias format", + "'%(alias)s' is not a valid format for an alias": "'%(alias)s' is not a valid format for an alias", + "Invalid address format": "Invalid address format", + "'%(alias)s' is not a valid format for an address": "'%(alias)s' is not a valid format for an address", + "not specified": "not specified", + "not set": "not set", + "Remote addresses for this room:": "Remote addresses for this room:", + "Addresses": "Addresses", + "The main address for this room is": "The main address for this room is", + "Local addresses for this room:": "Local addresses for this room:", + "This room has no local addresses": "This room has no local addresses", + "New address (e.g. #foo:%(localDomain)s)": "New address (e.g. #foo:%(localDomain)s)", + "Invalid community ID": "Invalid community ID", + "'%(groupId)s' is not a valid community ID": "'%(groupId)s' is not a valid community ID", + "Flair": "Flair", + "Showing flair for these communities:": "Showing flair for these communities:", + "This room is not showing flair for any communities": "This room is not showing flair for any communities", + "New community ID (e.g. +foo:%(localDomain)s)": "New community ID (e.g. +foo:%(localDomain)s)", + "You have enabled URL previews by default.": "You have enabled URL previews by default.", + "You have disabled URL previews by default.": "You have disabled URL previews by default.", + "URL previews are enabled by default for participants in this room.": "URL previews are enabled by default for participants in this room.", + "URL previews are disabled by default for participants in this room.": "URL previews are disabled by default for participants in this room.", + "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.": "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.", + "URL Previews": "URL Previews", + "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.": "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.", "Sunday": "Sunday", "Monday": "Monday", "Tuesday": "Tuesday", From 850a4d8ec5bc64c63de658de3593ae730afad3d9 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 16 Jul 2018 16:54:25 +0100 Subject: [PATCH 107/123] Jitsi: Check integrations server before call --- src/CallHandler.js | 25 +++++++++++++- src/i18n/strings/en_EN.json | 67 +++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/CallHandler.js b/src/CallHandler.js index 7403483e36..c76cff9b2e 100644 --- a/src/CallHandler.js +++ b/src/CallHandler.js @@ -62,6 +62,7 @@ import dis from './dispatcher'; import { showUnknownDeviceDialogForCalls } from './cryptodevices'; import SettingsStore from "./settings/SettingsStore"; import WidgetUtils from './utils/WidgetUtils'; +import ScalarAuthClient from './ScalarAuthClient'; global.mxCalls = { //room_id: MatrixCall @@ -401,7 +402,29 @@ function _onAction(payload) { } } -function _startCallApp(roomId, type) { +async function _startCallApp(roomId, type) { + // check for a working intgrations manager. Technically we could put + // the state event in anyway, but the resulting widget would then not + // work for us. Better that the user knows before everyone else in the + // room sees it. + const scalarClient = new ScalarAuthClient(); + let haveScalar = false; + try { + await scalarClient.connect(); + haveScalar = scalarClient.hasCredentials(); + } catch (e) { + // fall through + } + if (!haveScalar) { + const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); + + Modal.createTrackedDialog('Could not connect to the integration server', '', ErrorDialog, { + title: _t('Could not connect to the integration server'), + description: _t('A conference call could not be started because the intgrations server is not available'), + }); + return; + } + dis.dispatch({ action: 'appsDrawer', show: true, diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 360e076bd8..710e0b5ec8 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -39,15 +39,13 @@ "Conference calling is in development and may not be reliable.": "Conference calling is in development and may not be reliable.", "Failed to set up conference call": "Failed to set up conference call", "Conference call failed.": "Conference call failed.", + "Could not connect to the integration server": "Could not connect to the integration server", + "A conference call could not be started because the intgrations server is not available": "A conference call could not be started because the intgrations server is not available", "Call in Progress": "Call in Progress", "A call is already in progress!": "A call is already in progress!", "The file '%(fileName)s' failed to upload": "The file '%(fileName)s' failed to upload", "The file '%(fileName)s' exceeds this home server's size limit for uploads": "The file '%(fileName)s' exceeds this home server's size limit for uploads", "Upload Failed": "Upload Failed", - "Failure to create room": "Failure to create room", - "Server may be unavailable, overloaded, or you hit a bug.": "Server may be unavailable, overloaded, or you hit a bug.", - "Send anyway": "Send anyway", - "Send": "Send", "Sun": "Sun", "Mon": "Mon", "Tue": "Tue", @@ -87,7 +85,6 @@ "Failed to invite users to community": "Failed to invite users to community", "Failed to invite users to %(groupId)s": "Failed to invite users to %(groupId)s", "Failed to add the following rooms to %(groupId)s:": "Failed to add the following rooms to %(groupId)s:", - "Unnamed Room": "Unnamed Room", "Riot does not have permission to send you notifications - please check your browser settings": "Riot does not have permission to send you notifications - please check your browser settings", "Riot was not given permission to send notifications - please try again": "Riot was not given permission to send notifications - please try again", "Unable to enable Notifications": "Unable to enable Notifications", @@ -201,6 +198,11 @@ "%(names)s and %(count)s others are typing|other": "%(names)s and %(count)s others are typing", "%(names)s and %(count)s others are typing|one": "%(names)s and one other is typing", "%(names)s and %(lastPerson)s are typing": "%(names)s and %(lastPerson)s are typing", + "Failure to create room": "Failure to create room", + "Server may be unavailable, overloaded, or you hit a bug.": "Server may be unavailable, overloaded, or you hit a bug.", + "Send anyway": "Send anyway", + "Send": "Send", + "Unnamed Room": "Unnamed Room", "Your browser does not support the required cryptography extensions": "Your browser does not support the required cryptography extensions", "Not a valid Riot keyfile": "Not a valid Riot keyfile", "Authentication check failed: incorrect password?": "Authentication check failed: incorrect password?", @@ -314,31 +316,6 @@ "Off": "Off", "On": "On", "Noisy": "Noisy", - "Invalid alias format": "Invalid alias format", - "'%(alias)s' is not a valid format for an alias": "'%(alias)s' is not a valid format for an alias", - "Invalid address format": "Invalid address format", - "'%(alias)s' is not a valid format for an address": "'%(alias)s' is not a valid format for an address", - "not specified": "not specified", - "not set": "not set", - "Remote addresses for this room:": "Remote addresses for this room:", - "Addresses": "Addresses", - "The main address for this room is": "The main address for this room is", - "Local addresses for this room:": "Local addresses for this room:", - "This room has no local addresses": "This room has no local addresses", - "New address (e.g. #foo:%(localDomain)s)": "New address (e.g. #foo:%(localDomain)s)", - "Invalid community ID": "Invalid community ID", - "'%(groupId)s' is not a valid community ID": "'%(groupId)s' is not a valid community ID", - "Flair": "Flair", - "Showing flair for these communities:": "Showing flair for these communities:", - "This room is not showing flair for any communities": "This room is not showing flair for any communities", - "New community ID (e.g. +foo:%(localDomain)s)": "New community ID (e.g. +foo:%(localDomain)s)", - "You have enabled URL previews by default.": "You have enabled URL previews by default.", - "You have disabled URL previews by default.": "You have disabled URL previews by default.", - "URL previews are enabled by default for participants in this room.": "URL previews are enabled by default for participants in this room.", - "URL previews are disabled by default for participants in this room.": "URL previews are disabled by default for participants in this room.", - "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.": "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.", - "URL Previews": "URL Previews", - "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.": "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.", "Cannot add any more widgets": "Cannot add any more widgets", "The maximum permitted number of widgets have already been added to this room.": "The maximum permitted number of widgets have already been added to this room.", "Add a widget": "Add a widget", @@ -441,11 +418,11 @@ "At this time it is not possible to reply with an emote.": "At this time it is not possible to reply with an emote.", "Markdown is disabled": "Markdown is disabled", "Markdown is enabled": "Markdown is enabled", + "Unpin Message": "Unpin Message", + "Jump to message": "Jump to message", "No pinned messages.": "No pinned messages.", "Loading...": "Loading...", "Pinned Messages": "Pinned Messages", - "Unpin Message": "Unpin Message", - "Jump to message": "Jump to message", "%(duration)ss": "%(duration)ss", "%(duration)sm": "%(duration)sm", "%(duration)sh": "%(duration)sh", @@ -579,6 +556,31 @@ "Scroll to unread messages": "Scroll to unread messages", "Jump to first unread message.": "Jump to first unread message.", "Close": "Close", + "Invalid alias format": "Invalid alias format", + "'%(alias)s' is not a valid format for an alias": "'%(alias)s' is not a valid format for an alias", + "Invalid address format": "Invalid address format", + "'%(alias)s' is not a valid format for an address": "'%(alias)s' is not a valid format for an address", + "not specified": "not specified", + "not set": "not set", + "Remote addresses for this room:": "Remote addresses for this room:", + "Addresses": "Addresses", + "The main address for this room is": "The main address for this room is", + "Local addresses for this room:": "Local addresses for this room:", + "This room has no local addresses": "This room has no local addresses", + "New address (e.g. #foo:%(localDomain)s)": "New address (e.g. #foo:%(localDomain)s)", + "Invalid community ID": "Invalid community ID", + "'%(groupId)s' is not a valid community ID": "'%(groupId)s' is not a valid community ID", + "Flair": "Flair", + "Showing flair for these communities:": "Showing flair for these communities:", + "This room is not showing flair for any communities": "This room is not showing flair for any communities", + "New community ID (e.g. +foo:%(localDomain)s)": "New community ID (e.g. +foo:%(localDomain)s)", + "You have enabled URL previews by default.": "You have enabled URL previews by default.", + "You have disabled URL previews by default.": "You have disabled URL previews by default.", + "URL previews are enabled by default for participants in this room.": "URL previews are enabled by default for participants in this room.", + "URL previews are disabled by default for participants in this room.": "URL previews are disabled by default for participants in this room.", + "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.": "In encrypted rooms, like this one, URL previews are disabled by default to ensure that your homeserver (where the previews are generated) cannot gather information about links you see in this room.", + "URL Previews": "URL Previews", + "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.": "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.", "Sunday": "Sunday", "Monday": "Monday", "Tuesday": "Tuesday", @@ -710,7 +712,6 @@ "Uploaded on %(date)s by %(user)s": "Uploaded on %(date)s by %(user)s", "Download this file": "Download this file", "Integrations Error": "Integrations Error", - "Could not connect to the integration server": "Could not connect to the integration server", "Manage Integrations": "Manage Integrations", "%(nameList)s %(transitionList)s": "%(nameList)s %(transitionList)s", "%(severalUsers)sjoined %(count)s times|other": "%(severalUsers)sjoined %(count)s times", From 2f1779751ba2cf6fc2f5f15acc12b71e01a31de6 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 16:56:08 +0100 Subject: [PATCH 108/123] make the MessageComposerStore stateless for better performance and inter-tab composition (by simplifying it. WIN-WIN) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 10 +-- src/stores/MessageComposerStore.js | 79 ++++++------------- 2 files changed, 27 insertions(+), 62 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 89e4107a56..26e6ac8ab2 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -589,14 +589,8 @@ export default class MessageComposerInput extends React.Component { this.props.onInputStateChanged(inputState); } - // Record the editor state for this room so that it can be retrieved after - // switching to another room and back - dis.dispatch({ - action: 'editor_state', - room_id: this.props.room.roomId, - rich_text: this.state.isRichTextEnabled, - editor_state: editorState, - }); + // Record the editor state for this room so that it can be retrieved after switching to another room and back + MessageComposerStore.setEditorState(this.props.room.roomId, editorState, this.state.isRichTextEnabled); this.setState({ editorState, diff --git a/src/stores/MessageComposerStore.js b/src/stores/MessageComposerStore.js index 1d37a7c9e5..0b22057f3c 100644 --- a/src/stores/MessageComposerStore.js +++ b/src/stores/MessageComposerStore.js @@ -13,72 +13,43 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import dis from '../dispatcher'; -import { Store } from 'flux/utils'; import { Value } from 'slate'; -const INITIAL_STATE = { - // a map of room_id to rich text editor composer state - editorStateMap: localStorage.getItem('editor_state') ? - JSON.parse(localStorage.getItem('editor_state')) : {}, -}; +const localStoragePrefix = 'editor_state_'; /** - * A class for storing application state to do with the message composer (specifically - * in-progress message drafts). This is a simple - * flux store that listens for actions and updates its state accordingly, informing any - * listeners (views) of state changes. + * A class for storing application state to do with the message composer (specifically in-progress message drafts). + * It does not worry about cleaning up on log out as this is handled in Lifecycle.js by localStorage.clear() */ -class MessageComposerStore extends Store { +class MessageComposerStore { constructor() { - super(dis); - - // Initialise state - this._state = Object.assign({}, INITIAL_STATE); + this.prefix = localStoragePrefix; } - _setState(newState) { - this._state = Object.assign(this._state, newState); - this.__emitChange(); + _getKey(roomId: string): string { + return this.prefix + roomId; } - __onDispatch(payload) { - switch (payload.action) { - case 'editor_state': - this._editorState(payload); - break; - case 'on_logged_out': - this.reset(); - break; + setEditorState(roomId: string, editorState: Value, richText: boolean) { + localStorage.setItem(this._getKey(roomId), JSON.stringify({ + editor_state: editorState, + rich_text: richText, + })); + } + + getEditorState(roomId): {editor_state: Value, rich_text: boolean} { + const stateStr = localStorage.getItem(this._getKey(roomId)); + + let state; + if (stateStr) { + state = JSON.parse(stateStr); + + // if it does not have the fields we expect then bail + if (!state || state.rich_text === undefined || state.editor_state === undefined) return; + state.editor_state = Value.fromJSON(state.editor_state); } - } - _editorState(payload) { - const editorStateMap = this._state.editorStateMap; - editorStateMap[payload.room_id] = { - editor_state: payload.editor_state, - rich_text: payload.rich_text, - }; - localStorage.setItem('editor_state', JSON.stringify(editorStateMap)); - this._setState({ - editorStateMap: editorStateMap, - }); - } - - getEditorState(roomId) { - const editorStateMap = this._state.editorStateMap; - // const entry = this._state.editorStateMap[roomId]; - if (editorStateMap[roomId] && !Value.isValue(editorStateMap[roomId].editor_state)) { - // rehydrate lazily to prevent massive churn at launch and cache it - editorStateMap[roomId].editor_state = Value.fromJSON(editorStateMap[roomId].editor_state); - } - // explicitly don't setState here because the value didn't actually change, we just hydrated it, - // if a listener received an update they too would call this method and have a hydrated Value - return editorStateMap[roomId]; - } - - reset() { - this._state = Object.assign({}, INITIAL_STATE); + return state; } } From 74ad795d4a36aecb7438f9cc014b5b0ead81b6e0 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 16 Jul 2018 17:10:58 +0100 Subject: [PATCH 109/123] Fix read receipts on top of Jitsi By removing z-index which, as far as I can see, does nothing. Fixes https://github.com/vector-im/riot-web/issues/7021 --- res/css/views/rooms/_EventTile.scss | 1 - 1 file changed, 1 deletion(-) diff --git a/res/css/views/rooms/_EventTile.scss b/res/css/views/rooms/_EventTile.scss index c69c66ac7e..2898fb7bb7 100644 --- a/res/css/views/rooms/_EventTile.scss +++ b/res/css/views/rooms/_EventTile.scss @@ -186,7 +186,6 @@ limitations under the License. .mx_EventTile_msgOption { float: right; text-align: right; - z-index: 1; position: relative; width: 90px; From c7859fed1a49b452238e347f7740b5102b1f556e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 18:27:06 +0100 Subject: [PATCH 110/123] add heuristic to compare documents to prevent autocomplete flicker Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 26e6ac8ab2..26e66f25fc 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -498,8 +498,10 @@ export default class MessageComposerInput extends React.Component { } } - // when selection changes hide the autocomplete - if (!rangeEquals(this.state.editorState.selection, editorState.selection)) { + // when selection changes hide the autocomplete. + // Selection changes when we enter text so use a heuristic to compare documents without doing it recursively + const documentChanged = this.state.editorState.document.text !== editorState.document.text; + if (!documentChanged && !rangeEquals(this.state.editorState.selection, editorState.selection)) { this.autocomplete.hide(); } From bdbc2cbd38c1fabf4efe7b2a6de9d14a936a8a51 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 18:32:50 +0100 Subject: [PATCH 111/123] disable spellCheck when the placeholder is shown because "unencrypted" Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 26e66f25fc..84ee58925d 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1543,7 +1543,8 @@ export default class MessageComposerInput extends React.Component { onFocus={this.onFocus} renderNode={this.renderNode} renderMark={this.renderMark} - spellCheck={true} + // disable spell check for the placeholder because browsers don't like "unencrypted" + spellCheck={!this.state.editorState.document.isEmpty} />
    From 30000999913f0aee27fed99c7328a04c3bb7db8e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 21:27:52 +0100 Subject: [PATCH 112/123] add additional checks for hiding autocomplete to prevent `text` calculation Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 84ee58925d..38c544ce38 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -498,10 +498,12 @@ export default class MessageComposerInput extends React.Component { } } - // when selection changes hide the autocomplete. + // when in autocomplete mode and selection changes hide the autocomplete. // Selection changes when we enter text so use a heuristic to compare documents without doing it recursively - const documentChanged = this.state.editorState.document.text !== editorState.document.text; - if (!documentChanged && !rangeEquals(this.state.editorState.selection, editorState.selection)) { + if (this.autocomplete.state.completionList.length >= 0 && !this.autocomplete.state.hide && + this.state.editorState.document.text !== editorState.document.text && + !rangeEquals(this.state.editorState.selection, editorState.selection)) + { this.autocomplete.hide(); } From 547e0ed52c9be8b3e72462abdaf7244ba825b161 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 21:30:04 +0100 Subject: [PATCH 113/123] check if has some completions, not if >=0 Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 38c544ce38..f4befa8c98 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -500,7 +500,7 @@ export default class MessageComposerInput extends React.Component { // when in autocomplete mode and selection changes hide the autocomplete. // Selection changes when we enter text so use a heuristic to compare documents without doing it recursively - if (this.autocomplete.state.completionList.length >= 0 && !this.autocomplete.state.hide && + if (this.autocomplete.state.completionList.length > 0 && !this.autocomplete.state.hide && this.state.editorState.document.text !== editorState.document.text && !rangeEquals(this.state.editorState.selection, editorState.selection)) { From 1c24f1fd5a89f5021b30a43bbad41da6afb32fac Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 21:34:25 +0100 Subject: [PATCH 114/123] fix 2nd typo in one PR :( Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index f4befa8c98..5d6b12f339 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -501,7 +501,7 @@ export default class MessageComposerInput extends React.Component { // when in autocomplete mode and selection changes hide the autocomplete. // Selection changes when we enter text so use a heuristic to compare documents without doing it recursively if (this.autocomplete.state.completionList.length > 0 && !this.autocomplete.state.hide && - this.state.editorState.document.text !== editorState.document.text && + this.state.editorState.document.text === editorState.document.text && !rangeEquals(this.state.editorState.selection, editorState.selection)) { this.autocomplete.hide(); From cbe5944ff0c2339253c175121ba8fcc80313e257 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 22:04:42 +0100 Subject: [PATCH 115/123] preserve selection and history between room changes and refreshes Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/stores/MessageComposerStore.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/stores/MessageComposerStore.js b/src/stores/MessageComposerStore.js index 0b22057f3c..f180d931bf 100644 --- a/src/stores/MessageComposerStore.js +++ b/src/stores/MessageComposerStore.js @@ -32,7 +32,12 @@ class MessageComposerStore { setEditorState(roomId: string, editorState: Value, richText: boolean) { localStorage.setItem(this._getKey(roomId), JSON.stringify({ - editor_state: editorState, + editor_state: editorState.toJSON({ + preserveSelection: true, + preserveHistory: true, + // XXX: this seems like a workaround for selection.isSet being based on anchorKey instead of anchorPath + preserveKeys: true, + }), rich_text: richText, })); } From 23b6abf8b727e6c478f163087b3340aaf5e33314 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 Jul 2018 22:25:41 +0100 Subject: [PATCH 116/123] no point saving history at this point, maybe future supported Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/stores/MessageComposerStore.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stores/MessageComposerStore.js b/src/stores/MessageComposerStore.js index f180d931bf..ab2dbfedec 100644 --- a/src/stores/MessageComposerStore.js +++ b/src/stores/MessageComposerStore.js @@ -34,7 +34,8 @@ class MessageComposerStore { localStorage.setItem(this._getKey(roomId), JSON.stringify({ editor_state: editorState.toJSON({ preserveSelection: true, - preserveHistory: true, + // XXX: re-hydrating history is not currently supported by fromJSON + // preserveHistory: true, // XXX: this seems like a workaround for selection.isSet being based on anchorKey instead of anchorPath preserveKeys: true, }), From 5b79e3bcd52db91ac924f317850412140eee019f Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Jul 2018 11:56:24 +0100 Subject: [PATCH 117/123] fix user autocomplete not adding `: ` if at the start Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/autocomplete/UserProvider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autocomplete/UserProvider.js b/src/autocomplete/UserProvider.js index 7998337e2e..156aac2eb8 100644 --- a/src/autocomplete/UserProvider.js +++ b/src/autocomplete/UserProvider.js @@ -111,7 +111,7 @@ export default class UserProvider extends AutocompleteProvider { // relies on the length of the entity === length of the text in the decoration. completion: user.rawDisplayName.replace(' (IRC)', ''), completionId: user.userId, - suffix: (selection.beginning && selection.start === 0) ? ': ' : ' ', + suffix: (selection.beginning && range.start === 0) ? ': ' : ' ', href: makeUserPermalink(user.userId), component: ( Date: Tue, 17 Jul 2018 12:30:22 +0100 Subject: [PATCH 118/123] allow user to break out of blockquote/etc by hitting enter twice Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 5d6b12f339..2fae59a1fe 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -990,15 +990,29 @@ export default class MessageComposerInput extends React.Component { return change.insertText('\n'); } - if (this.state.editorState.blocks.some( - block => ['code', 'block-quote', 'list-item'].includes(block.type) - )) { + const editorState = this.state.editorState; + + let inBlock = false; + let inEmptyBlock = false; + + for (const block of editorState.blocks) { + if (['code', 'block-quote', 'list-item'].includes(block.type)) { + inBlock = true; + if (block.text === '') { + inEmptyBlock = true; + } + break; + } + } + + if (inEmptyBlock) { + // allow the user to cancel empty block by hitting return, useful in conjunction with below `inBlock` + return change.setBlocks(DEFAULT_NODE); + } else if (inBlock) { // allow the user to terminate blocks by hitting return rather than sending a msg return; } - const editorState = this.state.editorState; - let contentText; let contentHTML; From 8d4cead105a35a4ca23c3c5f281df6652f3ff551 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Jul 2018 13:24:28 +0100 Subject: [PATCH 119/123] special-case 'inline-code' button because mark is called 'code' Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposer.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 657fd463fd..a7e02d16ae 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -334,7 +334,9 @@ export default class MessageComposer extends React.Component { if (this.state.showFormatting && this.state.inputState.isRichTextEnabled) { const {marks, blockType} = this.state.inputState; const formatButtons = formatButtonList.map((name) => { - const active = marks.some(mark => mark.type === name) || blockType === name; + // special-case to match the md serializer and the special-case in MessageComposerInput.js + const markName = name === 'inline-code' ? 'code' : name; + const active = marks.some(mark => mark.type === markName) || blockType === name; const suffix = active ? '-on' : ''; const onFormatButtonClicked = this.onFormatButtonClicked.bind(this, name); const className = 'mx_MessageComposer_format_button mx_filterFlipColor'; From 688776bc10d43a1676921217126daa0e988c55c4 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Jul 2018 13:29:40 +0100 Subject: [PATCH 120/123] allow enter to remove current block if its empty useful for lists Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 2fae59a1fe..841fcccf06 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -992,24 +992,25 @@ export default class MessageComposerInput extends React.Component { const editorState = this.state.editorState; - let inBlock = false; - let inEmptyBlock = false; - - for (const block of editorState.blocks) { - if (['code', 'block-quote', 'list-item'].includes(block.type)) { - inBlock = true; - if (block.text === '') { - inEmptyBlock = true; - } - break; + const lastBlock = editorState.blocks.last(); + if (['code', 'block-quote', 'list-item'].includes(lastBlock.type)) { + const text = lastBlock.text; + if (text === '') { + // allow the user to cancel empty block by hitting return, useful in conjunction with below `inBlock` + return change + .setBlocks(DEFAULT_NODE) + .unwrapBlock('bulleted-list') + .unwrapBlock('numbered-list'); } - } - if (inEmptyBlock) { - // allow the user to cancel empty block by hitting return, useful in conjunction with below `inBlock` - return change.setBlocks(DEFAULT_NODE); - } else if (inBlock) { - // allow the user to terminate blocks by hitting return rather than sending a msg + // TODO strip trailing lines from blockquotes/list entries + // the below code seemingly works but doesn't account for edge cases like return with caret not at end + /* const trailingNewlines = text.match(/\n*$/); + if (trailingNewlines && trailingNewlines[0]) { + remove trailing newlines at the end of this block before making a new one + return change.deleteBackward(trailingNewlines[0].length); + }*/ + return; } From 414908611d5d688488e2c9c8d9e23e483a217725 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Jul 2018 13:48:15 +0100 Subject: [PATCH 121/123] prevent exception if there is no text node at anchor Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 841fcccf06..e4bd21cdf0 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -382,7 +382,8 @@ export default class MessageComposerInput extends React.Component { const quote = Block.create('block-quote'); if (this.state.isRichTextEnabled) { let change = editorState.change(); - if (editorState.anchorText.text === '' && editorState.anchorBlock.nodes.size === 1) { + const anchorText = editorState.anchorText; + if ((!anchorText || anchorText.text === '') && editorState.anchorBlock.nodes.size === 1) { // replace the current block rather than split the block change = change.replaceNodeByKey(editorState.anchorBlock.key, quote); } From 1b467f6ca85185fc1d51bb029f80a0f6f6766d21 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Jul 2018 16:02:37 +0100 Subject: [PATCH 122/123] fix pasting of plaintext content (prevent loads of

    s) Fixes vector-im/riot-web#7043 Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index e4bd21cdf0..5885ae4e6a 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -970,19 +970,21 @@ export default class MessageComposerInput extends React.Component { onPaste = (event: Event, change: Change, editor: Editor): Change => { const transfer = getEventTransfer(event); - if (transfer.type === "files") { - return this.props.onFilesPasted(transfer.files); - } - else if (transfer.type === "html") { - // FIXME: https://github.com/ianstormtaylor/slate/issues/1497 means - // that we will silently discard nested blocks (e.g. nested lists) :( - const fragment = this.html.deserialize(transfer.html); - if (this.state.isRichTextEnabled) { - return change.insertFragment(fragment.document); - } - else { - return change.insertText(this.md.serialize(fragment)); + switch (transfer.type) { + case 'files': + return this.props.onFilesPasted(transfer.files); + case 'html': { + // FIXME: https://github.com/ianstormtaylor/slate/issues/1497 means + // that we will silently discard nested blocks (e.g. nested lists) :( + const fragment = this.html.deserialize(transfer.html); + if (this.state.isRichTextEnabled) { + return change.insertFragment(fragment.document); + } else { + return change.insertText(this.md.serialize(fragment)); + } } + case 'text': + return change.insertText(transfer.text); } }; From 720a728278a3eeac27f8732ad3a510dcfd832e9b Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Jul 2018 16:13:33 +0100 Subject: [PATCH 123/123] workaround for placeholder being shown when empty text but block. Fixes vector-im/riot-web#7042 Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 5885ae4e6a..1850601d8a 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -1533,6 +1533,14 @@ export default class MessageComposerInput extends React.Component { mx_MessageComposer_input_error: this.state.someCompletions === false, }); + const isEmpty = this.state.editorState.document.isEmpty; + + let {placeholder} = this.props; + // XXX: workaround for placeholder being shown when there is a formatting block e.g blockquote but no text + if (isEmpty && this.state.editorState.startBlock.type !== DEFAULT_NODE) { + placeholder = undefined; + } + return (

    @@ -1554,7 +1562,7 @@ export default class MessageComposerInput extends React.Component {