2019-05-06 19:21:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 17:16:32 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-06 19:21:28 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-05-09 15:59:52 +03:00
|
|
|
import AutocompleteWrapperModel from "./autocomplete";
|
2019-05-17 21:49:46 +03:00
|
|
|
import Avatar from "../Avatar";
|
2019-05-09 15:59:52 +03:00
|
|
|
|
2019-05-06 19:21:28 +03:00
|
|
|
class BasePart {
|
|
|
|
constructor(text = "") {
|
|
|
|
this._text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptsInsertion(chr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptsRemoval(position, chr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
merge(part) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
split(offset) {
|
|
|
|
const splitText = this.text.substr(offset);
|
|
|
|
this._text = this.text.substr(0, offset);
|
|
|
|
return new PlainPart(splitText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// removes len chars, or returns the plain text this part should be replaced with
|
|
|
|
// if the part would become invalid if it removed everything.
|
|
|
|
remove(offset, len) {
|
|
|
|
// validate
|
|
|
|
const strWithRemoval = this.text.substr(0, offset) + this.text.substr(offset + len);
|
2019-05-09 15:59:52 +03:00
|
|
|
for (let i = offset; i < (len + offset); ++i) {
|
2019-05-06 19:21:28 +03:00
|
|
|
const chr = this.text.charAt(i);
|
|
|
|
if (!this.acceptsRemoval(i, chr)) {
|
|
|
|
return strWithRemoval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._text = strWithRemoval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// append str, returns the remaining string if a character was rejected.
|
|
|
|
appendUntilRejected(str) {
|
2019-05-09 15:59:52 +03:00
|
|
|
for (let i = 0; i < str.length; ++i) {
|
2019-05-06 19:21:28 +03:00
|
|
|
const chr = str.charAt(i);
|
2019-05-16 20:13:48 +03:00
|
|
|
if (!this.acceptsInsertion(chr, i)) {
|
2019-05-06 19:21:28 +03:00
|
|
|
this._text = this._text + str.substr(0, i);
|
|
|
|
return str.substr(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._text = this._text + str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inserts str at offset if all the characters in str were accepted, otherwise don't do anything
|
|
|
|
// return whether the str was accepted or not.
|
2019-07-11 19:32:58 +03:00
|
|
|
validateAndInsert(offset, str) {
|
2019-05-09 15:59:52 +03:00
|
|
|
for (let i = 0; i < str.length; ++i) {
|
2019-05-06 19:21:28 +03:00
|
|
|
const chr = str.charAt(i);
|
|
|
|
if (!this.acceptsInsertion(chr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const beforeInsert = this._text.substr(0, offset);
|
|
|
|
const afterInsert = this._text.substr(offset);
|
|
|
|
this._text = beforeInsert + str + afterInsert;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-11 19:32:58 +03:00
|
|
|
insert(offset, str) {
|
|
|
|
if (this.canEdit) {
|
|
|
|
const beforeInsert = this._text.substr(0, offset);
|
|
|
|
const afterInsert = this._text.substr(offset);
|
|
|
|
this._text = beforeInsert + str + afterInsert;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-09 15:59:52 +03:00
|
|
|
createAutoComplete() {}
|
2019-05-06 19:21:28 +03:00
|
|
|
|
|
|
|
trim(len) {
|
|
|
|
const remaining = this._text.substr(len);
|
|
|
|
this._text = this._text.substr(0, len);
|
|
|
|
return remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
get text() {
|
|
|
|
return this._text;
|
|
|
|
}
|
2019-05-09 16:42:10 +03:00
|
|
|
|
|
|
|
get canEdit() {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-13 17:21:57 +03:00
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `${this.type}(${this.text})`;
|
|
|
|
}
|
2019-06-12 19:32:32 +03:00
|
|
|
|
|
|
|
serialize() {
|
|
|
|
return {type: this.type, text: this.text};
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 13:16:34 +03:00
|
|
|
class PlainPart extends BasePart {
|
2019-05-06 19:21:28 +03:00
|
|
|
acceptsInsertion(chr) {
|
2019-05-13 17:21:57 +03:00
|
|
|
return chr !== "@" && chr !== "#" && chr !== ":" && chr !== "\n";
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toDOMNode() {
|
|
|
|
return document.createTextNode(this.text);
|
|
|
|
}
|
|
|
|
|
|
|
|
merge(part) {
|
|
|
|
if (part.type === this.type) {
|
|
|
|
this._text = this.text + part.text;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return "plain";
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDOMNode(node) {
|
|
|
|
if (node.textContent !== this.text) {
|
|
|
|
// console.log("changing plain text from", node.textContent, "to", this.text);
|
|
|
|
node.textContent = this.text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canUpdateDOMNode(node) {
|
|
|
|
return node.nodeType === Node.TEXT_NODE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PillPart extends BasePart {
|
2019-05-09 15:59:52 +03:00
|
|
|
constructor(resourceId, label) {
|
|
|
|
super(label);
|
|
|
|
this.resourceId = resourceId;
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:21:28 +03:00
|
|
|
acceptsInsertion(chr) {
|
|
|
|
return chr !== " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptsRemoval(position, chr) {
|
|
|
|
return position !== 0; //if you remove initial # or @, pill should become plain
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOMNode() {
|
|
|
|
const container = document.createElement("span");
|
2019-05-21 13:19:35 +03:00
|
|
|
container.className = this.className;
|
2019-05-06 19:21:28 +03:00
|
|
|
container.appendChild(document.createTextNode(this.text));
|
2019-05-20 15:21:25 +03:00
|
|
|
this.setAvatar(container);
|
2019-05-06 19:21:28 +03:00
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDOMNode(node) {
|
|
|
|
const textNode = node.childNodes[0];
|
|
|
|
if (textNode.textContent !== this.text) {
|
|
|
|
textNode.textContent = this.text;
|
|
|
|
}
|
2019-05-21 13:19:35 +03:00
|
|
|
if (node.className !== this.className) {
|
|
|
|
node.className = this.className;
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2019-05-20 15:21:25 +03:00
|
|
|
this.setAvatar(node);
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
canUpdateDOMNode(node) {
|
|
|
|
return node.nodeType === Node.ELEMENT_NODE &&
|
|
|
|
node.nodeName === "SPAN" &&
|
|
|
|
node.childNodes.length === 1 &&
|
|
|
|
node.childNodes[0].nodeType === Node.TEXT_NODE;
|
|
|
|
}
|
2019-05-09 16:42:10 +03:00
|
|
|
|
2019-05-21 13:19:35 +03:00
|
|
|
// helper method for subclasses
|
|
|
|
_setAvatarVars(node, avatarUrl, initialLetter) {
|
|
|
|
const avatarBackground = `url('${avatarUrl}')`;
|
|
|
|
const avatarLetter = `'${initialLetter}'`;
|
|
|
|
// check if the value is changing,
|
|
|
|
// otherwise the avatars flicker on every keystroke while updating.
|
|
|
|
if (node.style.getPropertyValue("--avatar-background") !== avatarBackground) {
|
|
|
|
node.style.setProperty("--avatar-background", avatarBackground);
|
|
|
|
}
|
|
|
|
if (node.style.getPropertyValue("--avatar-letter") !== avatarLetter) {
|
|
|
|
node.style.setProperty("--avatar-letter", avatarLetter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 16:42:10 +03:00
|
|
|
get canEdit() {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 13:16:34 +03:00
|
|
|
class NewlinePart extends BasePart {
|
2019-05-16 20:13:48 +03:00
|
|
|
acceptsInsertion(chr, i) {
|
|
|
|
return (this.text.length + i) === 0 && chr === "\n";
|
2019-05-13 17:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
acceptsRemoval(position, chr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOMNode() {
|
|
|
|
return document.createElement("br");
|
|
|
|
}
|
|
|
|
|
|
|
|
merge() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDOMNode() {}
|
|
|
|
|
|
|
|
canUpdateDOMNode(node) {
|
|
|
|
return node.tagName === "BR";
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return "newline";
|
|
|
|
}
|
2019-05-16 19:58:22 +03:00
|
|
|
|
|
|
|
// this makes the cursor skip this part when it is inserted
|
|
|
|
// rather than trying to append to it, which is what we want.
|
|
|
|
// As a newline can also be only one character, it makes sense
|
|
|
|
// as it can only be one character long. This caused #9741.
|
|
|
|
get canEdit() {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-13 17:21:57 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 13:16:34 +03:00
|
|
|
class RoomPillPart extends PillPart {
|
|
|
|
constructor(displayAlias, room) {
|
2019-05-09 15:59:52 +03:00
|
|
|
super(displayAlias, displayAlias);
|
2019-06-14 13:16:34 +03:00
|
|
|
this._room = room;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
|
|
|
|
2019-05-20 15:21:25 +03:00
|
|
|
setAvatar(node) {
|
2019-05-20 16:34:15 +03:00
|
|
|
let initialLetter = "";
|
2019-05-21 13:24:12 +03:00
|
|
|
let avatarUrl = Avatar.avatarUrlForRoom(this._room, 16 * window.devicePixelRatio, 16 * window.devicePixelRatio);
|
2019-05-20 16:34:15 +03:00
|
|
|
if (!avatarUrl) {
|
|
|
|
initialLetter = Avatar.getInitialLetter(this._room.name);
|
|
|
|
avatarUrl = `../../${Avatar.defaultAvatarUrlForString(this._room.roomId)}`;
|
|
|
|
}
|
2019-05-21 13:19:35 +03:00
|
|
|
this._setAvatarVars(node, avatarUrl, initialLetter);
|
2019-05-20 15:21:25 +03:00
|
|
|
}
|
|
|
|
|
2019-05-06 19:21:28 +03:00
|
|
|
get type() {
|
|
|
|
return "room-pill";
|
|
|
|
}
|
2019-05-21 13:19:35 +03:00
|
|
|
|
|
|
|
get className() {
|
|
|
|
return "mx_RoomPill mx_Pill";
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:24:18 +03:00
|
|
|
class AtRoomPillPart extends RoomPillPart {
|
|
|
|
get type() {
|
|
|
|
return "at-room-pill";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 13:16:34 +03:00
|
|
|
class UserPillPart extends PillPart {
|
2019-05-17 21:48:05 +03:00
|
|
|
constructor(userId, displayName, member) {
|
|
|
|
super(userId, displayName);
|
|
|
|
this._member = member;
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:21:25 +03:00
|
|
|
setAvatar(node) {
|
|
|
|
const name = this._member.name || this._member.userId;
|
|
|
|
const defaultAvatarUrl = Avatar.defaultAvatarUrlForString(this._member.userId);
|
2019-05-21 14:49:43 +03:00
|
|
|
let avatarUrl = Avatar.avatarUrlForMember(
|
|
|
|
this._member,
|
|
|
|
16 * window.devicePixelRatio,
|
|
|
|
16 * window.devicePixelRatio);
|
2019-05-20 15:21:25 +03:00
|
|
|
let initialLetter = "";
|
|
|
|
if (avatarUrl === defaultAvatarUrl) {
|
|
|
|
// the url from defaultAvatarUrlForString is meant to go in an img element,
|
|
|
|
// which has the base of the document. we're using it in css,
|
|
|
|
// which has the base of the theme css file, two levels deeper than the document,
|
|
|
|
// so go up to the level of the document.
|
|
|
|
avatarUrl = `../../${avatarUrl}`;
|
|
|
|
initialLetter = Avatar.getInitialLetter(name);
|
2019-05-17 21:49:46 +03:00
|
|
|
}
|
2019-05-21 13:19:35 +03:00
|
|
|
this._setAvatarVars(node, avatarUrl, initialLetter);
|
2019-05-17 21:49:46 +03:00
|
|
|
}
|
|
|
|
|
2019-05-06 19:21:28 +03:00
|
|
|
get type() {
|
|
|
|
return "user-pill";
|
|
|
|
}
|
2019-05-21 13:19:35 +03:00
|
|
|
|
|
|
|
get className() {
|
|
|
|
return "mx_UserPill mx_Pill";
|
|
|
|
}
|
2019-06-12 19:32:32 +03:00
|
|
|
|
|
|
|
serialize() {
|
|
|
|
const obj = super.serialize();
|
|
|
|
obj.userId = this.resourceId;
|
|
|
|
return obj;
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2019-05-09 15:59:52 +03:00
|
|
|
|
|
|
|
|
2019-06-14 13:16:34 +03:00
|
|
|
class PillCandidatePart extends PlainPart {
|
2019-05-09 15:59:52 +03:00
|
|
|
constructor(text, autoCompleteCreator) {
|
|
|
|
super(text);
|
|
|
|
this._autoCompleteCreator = autoCompleteCreator;
|
|
|
|
}
|
|
|
|
|
|
|
|
createAutoComplete(updateCallback) {
|
|
|
|
return this._autoCompleteCreator(updateCallback);
|
|
|
|
}
|
|
|
|
|
2019-05-24 13:38:19 +03:00
|
|
|
acceptsInsertion(chr, i) {
|
2019-05-24 16:43:55 +03:00
|
|
|
if ((this.text.length + i) === 0) {
|
2019-05-24 13:38:19 +03:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return super.acceptsInsertion(chr, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
merge() {
|
|
|
|
return false;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
acceptsRemoval(position, chr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return "pill-candidate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PartCreator {
|
2019-06-12 19:32:32 +03:00
|
|
|
constructor(getAutocompleterComponent, updateQuery, room, client) {
|
|
|
|
this._room = room;
|
|
|
|
this._client = client;
|
2019-05-09 15:59:52 +03:00
|
|
|
this._autoCompleteCreator = (updateCallback) => {
|
2019-05-17 21:48:05 +03:00
|
|
|
return new AutocompleteWrapperModel(
|
|
|
|
updateCallback,
|
|
|
|
getAutocompleterComponent,
|
|
|
|
updateQuery,
|
2019-06-14 13:16:34 +03:00
|
|
|
this,
|
2019-05-17 21:48:05 +03:00
|
|
|
);
|
2019-05-09 15:59:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
createPartForInput(input) {
|
|
|
|
switch (input[0]) {
|
|
|
|
case "#":
|
|
|
|
case "@":
|
|
|
|
case ":":
|
2019-06-14 13:16:34 +03:00
|
|
|
return this.pillCandidate("");
|
2019-05-13 17:21:57 +03:00
|
|
|
case "\n":
|
|
|
|
return new NewlinePart();
|
2019-05-09 15:59:52 +03:00
|
|
|
default:
|
|
|
|
return new PlainPart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createDefaultPart(text) {
|
2019-06-14 13:16:34 +03:00
|
|
|
return this.plain(text);
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
2019-06-12 19:32:32 +03:00
|
|
|
|
|
|
|
deserializePart(part) {
|
|
|
|
switch (part.type) {
|
|
|
|
case "plain":
|
2019-06-14 13:16:34 +03:00
|
|
|
return this.plain(part.text);
|
2019-06-12 19:32:32 +03:00
|
|
|
case "newline":
|
2019-06-14 13:16:34 +03:00
|
|
|
return this.newline();
|
2019-06-14 19:42:30 +03:00
|
|
|
case "at-room-pill":
|
|
|
|
return this.atRoomPill(part.text);
|
2019-06-12 19:32:32 +03:00
|
|
|
case "pill-candidate":
|
2019-06-14 13:16:34 +03:00
|
|
|
return this.pillCandidate(part.text);
|
2019-06-12 19:32:32 +03:00
|
|
|
case "room-pill":
|
2019-06-14 13:16:34 +03:00
|
|
|
return this.roomPill(part.text);
|
|
|
|
case "user-pill":
|
|
|
|
return this.userPill(part.text, part.userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plain(text) {
|
|
|
|
return new PlainPart(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
newline() {
|
|
|
|
return new NewlinePart("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
pillCandidate(text) {
|
|
|
|
return new PillCandidatePart(text, this._autoCompleteCreator);
|
|
|
|
}
|
|
|
|
|
|
|
|
roomPill(alias) {
|
|
|
|
let room;
|
|
|
|
if (alias[0] === '#') {
|
|
|
|
room = this._client.getRooms().find((r) => {
|
|
|
|
return r.getAliases().includes(alias);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
room = this._client.getRoom(alias);
|
2019-06-12 19:32:32 +03:00
|
|
|
}
|
2019-06-14 13:16:34 +03:00
|
|
|
return new RoomPillPart(alias, room);
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:24:18 +03:00
|
|
|
atRoomPill(text) {
|
|
|
|
return new AtRoomPillPart(text, this._room);
|
|
|
|
}
|
|
|
|
|
2019-06-14 13:16:34 +03:00
|
|
|
userPill(displayName, userId) {
|
|
|
|
const member = this._room.getMember(userId);
|
|
|
|
return new UserPillPart(userId, displayName, member);
|
2019-06-12 19:32:32 +03:00
|
|
|
}
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
|
|
|
|