Skip to content

Commit

Permalink
Merge pull request #921 from vector-im/madlittlemods/copy-permalink
Browse files Browse the repository at this point in the history
Add "Copy `matrix.to` permalink" message action
  • Loading branch information
bwindels authored Apr 11, 2023
2 parents ee5105c + 98d4dfd commit a8d4928
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 39 deletions.
5 changes: 5 additions & 0 deletions src/domain/session/room/timeline/tiles/BaseMessageTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {SimpleTile} from "./SimpleTile.js";
import {ReactionsViewModel} from "../ReactionsViewModel.js";
import {getIdentifierColorNumber, avatarInitials, getAvatarHttpUrl} from "../../../../avatar";


export class BaseMessageTile extends SimpleTile {
constructor(entry, options) {
super(entry, options);
Expand All @@ -44,6 +45,10 @@ export class BaseMessageTile extends SimpleTile {
return `https://matrix.to/#/${encodeURIComponent(this._room.id)}/${encodeURIComponent(this._entry.id)}`;
}

copyPermalink() {
this.platform.copyPlaintext(this.permaLink);
}

get senderProfileLink() {
return `https://matrix.to/#/${encodeURIComponent(this.sender)}`;
}
Expand Down
7 changes: 5 additions & 2 deletions src/matrix/room/PowerLevels.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ limitations under the License.

export const EVENT_TYPE = "m.room.power_levels";

// See https://spec.matrix.org/latest/client-server-api/#mroompower_levels
const STATE_DEFAULT_POWER_LEVEL = 50;

export class PowerLevels {
constructor({powerLevelEvent, createEvent, ownUserId, membership}) {
this._plEvent = powerLevelEvent;
Expand Down Expand Up @@ -66,11 +69,11 @@ export class PowerLevels {

/** @param {string} action either "invite", "kick", "ban" or "redact". */
_getActionLevel(action) {
const level = this._plEvent?.content[action];
const level = this._plEvent?.content?.[action];
if (typeof level === "number") {
return level;
} else {
return 50;
return STATE_DEFAULT_POWER_LEVEL;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/platform/web/Platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {MediaDevicesWrapper} from "./dom/MediaDevices";
import {DOMWebRTC} from "./dom/WebRTC";
import {ThemeLoader} from "./theming/ThemeLoader";
import {TimeFormatter} from "./dom/TimeFormatter";
import {copyPlaintext} from "./dom/utils";

function addScript(src) {
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -283,6 +284,10 @@ export class Platform {
}
}

async copyPlaintext(text) {
return await copyPlaintext(text);
}

restart() {
document.location.reload();
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/web/dom/ImageHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import {BlobHandle} from "./BlobHandle.js";
import {domEventAsPromise} from "./utils.js";
import {domEventAsPromise} from "./utils";

export class ImageHandle {
static async fromBlob(blob) {
Expand Down
35 changes: 0 additions & 35 deletions src/platform/web/dom/utils.js

This file was deleted.

79 changes: 79 additions & 0 deletions src/platform/web/dom/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
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.
*/

export function domEventAsPromise(element: HTMLElement, successEvent: string): Promise<void> {
return new Promise((resolve, reject) => {
let detach;
const handleError = evt => {
detach();
reject(evt.target.error);
};
const handleSuccess = () => {
detach();
resolve();
};
detach = () => {
element.removeEventListener(successEvent, handleSuccess);
element.removeEventListener("error", handleError);
};
element.addEventListener(successEvent, handleSuccess);
element.addEventListener("error", handleError);
});
}

// Copies the given text to clipboard and returns a boolean of whether the action was
// successful
export async function copyPlaintext(text: string): Promise<boolean> {
try {
if (navigator?.clipboard?.writeText) {
await navigator.clipboard.writeText(text);
return true;
} else {
const textArea = document.createElement("textarea");
textArea.value = text;

// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";

document.body.appendChild(textArea);

const selection = document.getSelection();
if (!selection) {
console.error('copyPlaintext: Unable to copy text to clipboard in fallback mode because `selection` was null/undefined');
return false;
}

const range = document.createRange();
// range.selectNodeContents(textArea);
range.selectNode(textArea);
selection.removeAllRanges();
selection.addRange(range);

const successful = document.execCommand("copy");
selection.removeAllRanges();
document.body.removeChild(textArea);
if(!successful) {
console.error('copyPlaintext: Unable to copy text to clipboard in fallback mode because the `copy` command is unsupported or disabled');
}
return successful;
}
} catch (err) {
console.error("copyPlaintext: Ran into an error", err);
}
return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class BaseMessageView extends TemplateView {
} else if (vm.canRedact) {
options.push(Menu.option(vm.i18n`Delete`, () => vm.redact()).setDestructive());
}
options.push(Menu.option(vm.i18n`Copy matrix.to permalink`, () => vm.copyPermalink()));
return options;
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/web/ui/session/room/timeline/VideoView.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import {BaseMediaView} from "./BaseMediaView.js";
import {domEventAsPromise} from "../../../../dom/utils.js";
import {domEventAsPromise} from "../../../../dom/utils";

export class VideoView extends BaseMediaView {
renderMedia(t) {
Expand Down

0 comments on commit a8d4928

Please sign in to comment.