mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2025-01-11 04:27:40 +01:00
Add copy permalink action
This commit is contained in:
parent
dbac61f78f
commit
087a4ad7ce
@ -17,6 +17,8 @@ limitations under the License.
|
|||||||
import {SimpleTile} from "./SimpleTile.js";
|
import {SimpleTile} from "./SimpleTile.js";
|
||||||
import {ReactionsViewModel} from "../ReactionsViewModel.js";
|
import {ReactionsViewModel} from "../ReactionsViewModel.js";
|
||||||
import {getIdentifierColorNumber, avatarInitials, getAvatarHttpUrl} from "../../../../avatar";
|
import {getIdentifierColorNumber, avatarInitials, getAvatarHttpUrl} from "../../../../avatar";
|
||||||
|
import {copyPlaintext} from "../../../../../platform/web/dom/utils";
|
||||||
|
|
||||||
|
|
||||||
export class BaseMessageTile extends SimpleTile {
|
export class BaseMessageTile extends SimpleTile {
|
||||||
constructor(entry, options) {
|
constructor(entry, options) {
|
||||||
@ -45,6 +47,10 @@ export class BaseMessageTile extends SimpleTile {
|
|||||||
return `https://matrix.to/#/${encodeURIComponent(this._room.id)}/${encodeURIComponent(this._entry.id)}`;
|
return `https://matrix.to/#/${encodeURIComponent(this._room.id)}/${encodeURIComponent(this._entry.id)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
copyPermalink() {
|
||||||
|
copyPlaintext(this.permaLink);
|
||||||
|
}
|
||||||
|
|
||||||
get senderProfileLink() {
|
get senderProfileLink() {
|
||||||
return `https://matrix.to/#/${encodeURIComponent(this.sender)}`;
|
return `https://matrix.to/#/${encodeURIComponent(this.sender)}`;
|
||||||
}
|
}
|
||||||
|
@ -66,10 +66,11 @@ export class PowerLevels {
|
|||||||
|
|
||||||
/** @param {string} action either "invite", "kick", "ban" or "redact". */
|
/** @param {string} action either "invite", "kick", "ban" or "redact". */
|
||||||
_getActionLevel(action) {
|
_getActionLevel(action) {
|
||||||
const level = this._plEvent?.content[action];
|
const level = this._plEvent?.content?.[action];
|
||||||
if (typeof level === "number") {
|
if (typeof level === "number") {
|
||||||
return level;
|
return level;
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: Why does this default to 50?
|
||||||
return 50;
|
return 50;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {BlobHandle} from "./BlobHandle.js";
|
import {BlobHandle} from "./BlobHandle.js";
|
||||||
import {domEventAsPromise} from "./utils.js";
|
import {domEventAsPromise} from "./utils";
|
||||||
|
|
||||||
export class ImageHandle {
|
export class ImageHandle {
|
||||||
static async fromBlob(blob) {
|
static async fromBlob(blob) {
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
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, successEvent) {
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
79
src/platform/web/dom/utils.ts
Normal file
79
src/platform/web/dom/utils.ts
Normal file
@ -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, successEvent): 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;
|
||||||
|
}
|
@ -120,6 +120,8 @@ export class BaseMessageView extends TemplateView {
|
|||||||
} else if (vm.canRedact) {
|
} else if (vm.canRedact) {
|
||||||
options.push(Menu.option(vm.i18n`Delete`, () => vm.redact()).setDestructive());
|
options.push(Menu.option(vm.i18n`Delete`, () => vm.redact()).setDestructive());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options.push(Menu.option(vm.i18n`Copy permalink`, () => vm.copyPermalink()));
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {BaseMediaView} from "./BaseMediaView.js";
|
import {BaseMediaView} from "./BaseMediaView.js";
|
||||||
import {domEventAsPromise} from "../../../../dom/utils.js";
|
import {domEventAsPromise} from "../../../../dom/utils";
|
||||||
|
|
||||||
export class VideoView extends BaseMediaView {
|
export class VideoView extends BaseMediaView {
|
||||||
renderMedia(t) {
|
renderMedia(t) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user