Notes/src/public/app/widgets/shared_info.js

72 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-12-20 17:30:47 +01:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import options from "../services/options.js";
2021-12-22 10:57:02 +01:00
import attributeService from "../services/attributes.js";
2024-09-08 21:19:13 +03:00
import { t } from "../services/i18n.js";
2021-12-20 17:30:47 +01:00
const TPL = `
<div class="shared-info-widget alert alert-warning use-tn-links">
2021-12-20 17:30:47 +01:00
<style>
.shared-info-widget {
margin: 10px;
contain: none;
padding: 10px;
font-weight: bold;
}
</style>
2024-09-08 21:19:13 +03:00
<span class="shared-text"></span> <a class="shared-link external"></a>. ${t("shared_info.help_link")}
2021-12-20 17:30:47 +01:00
</div>`;
export default class SharedInfoWidget extends NoteContextAwareWidget {
isEnabled() {
2025-01-09 18:07:02 +02:00
return super.isEnabled() && this.noteId !== "_share" && this.note.hasAncestor("_share");
2021-12-20 17:30:47 +01:00
}
doRender() {
this.$widget = $(TPL);
this.$sharedLink = this.$widget.find(".shared-link");
this.$sharedText = this.$widget.find(".shared-text");
2021-12-20 17:30:47 +01:00
this.contentSized();
}
async refreshWithNote(note) {
const syncServerHost = options.get("syncServerHost");
let link;
const shareId = this.getShareId(note);
2021-12-22 10:57:02 +01:00
2021-12-20 17:30:47 +01:00
if (syncServerHost) {
link = `${syncServerHost}/share/${shareId}`;
2024-09-08 21:19:13 +03:00
this.$sharedText.text(t("shared_info.shared_publicly"));
2025-01-09 18:07:02 +02:00
} else {
let host = location.host;
2025-01-09 18:07:02 +02:00
if (host.endsWith("/")) {
// seems like IE has trailing slash
// https://github.com/zadam/trilium/issues/3782
host = host.substr(0, host.length - 1);
}
link = `${location.protocol}//${host}${location.pathname}share/${shareId}`;
2024-09-08 21:19:13 +03:00
this.$sharedText.text(t("shared_info.shared_locally"));
2021-12-20 17:30:47 +01:00
}
this.$sharedLink.attr("href", link).text(link);
2021-12-20 17:30:47 +01:00
}
2021-12-22 10:57:02 +01:00
getShareId(note) {
2025-01-09 18:07:02 +02:00
if (note.hasOwnedLabel("shareRoot")) {
return "";
}
2025-01-09 18:07:02 +02:00
return note.getOwnedLabelValue("shareAlias") || note.noteId;
}
2025-01-09 18:07:02 +02:00
entitiesReloadedEvent({ loadResults }) {
if (loadResults.getAttributeRows().find((attr) => attr.name.startsWith("_share") && attributeService.isAffecting(attr, this.note))) {
2021-12-22 10:57:02 +01:00
this.refresh();
2025-01-09 18:07:02 +02:00
} else if (loadResults.getBranchRows().find((branch) => branch.noteId === this.noteId)) {
2021-12-22 15:01:54 +01:00
this.refresh();
}
2021-12-22 10:57:02 +01:00
}
2021-12-20 17:30:47 +01:00
}