Skip to content

Commit

Permalink
feat: ✨ Delete notes, sidebar leaf
Browse files Browse the repository at this point in the history
  • Loading branch information
mcndt committed Dec 4, 2022
1 parent 7a645c3 commit 5921345
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.3.0]

- feat: You can now unshare notes before they expire from the server.
- feat: Quickshare now has a sidebar panel to visually see your recently shared notes.

## [1.2.0]

- feat: the plugin now shares the note's name as title (like Obsidian's "show inline title" option in the appearance settings). This behavior can be disabled in the plugin's settings.
Expand Down
36 changes: 19 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class NoteSharingPlugin extends Plugin {
if (checking) {
return true;
}
this.deleteNote(activeView.file);
this.deleteNote(activeView.file.path);
},
});
}
Expand Down Expand Up @@ -224,35 +224,37 @@ export default class NoteSharingPlugin extends Plugin {
.catch(this.handleSharingError);
}

async deleteNote(file: TFile | string) {
async deleteNote(fileId: string) {
const { setFrontmatterKeys } = useFrontmatterHelper(this.app);

const _file =
typeof file === "string"
? this.app.vault.getMarkdownFiles().find((f) => f.path === file)
: file;
const cacheData = this.cache.get(fileId);

if (!_file) {
return;
}

const cacheData = this.cache.get(_file.path);
if (!cacheData) {
return;
}

this.noteSharingService
.deleteNote(cacheData.note_id, cacheData.secret_token)
.then(() => {
this.cache.set(fileId, (data) => ({
...data,
deleted_from_server: true,
}));
new Notice(`Unshared note: "${cacheData.basename}"`, 7500);
console.info("Unshared note: ", fileId);

const _file = this.app.vault
.getMarkdownFiles()
.find((f) => f.path === fileId);

if (!_file) {
return;
}

setFrontmatterKeys(_file, {
url: `"Removed"`,
datetime: `"N/A"`,
});
this.cache.set(_file.path, (data) => ({
...data,
deleted_from_server: true,
}));
new Notice(`Unshared note: "${_file.basename}"`, 7500);
console.info("Unshared note: ", _file.path);
})
.catch(this.handleSharingError);
}
Expand Down
9 changes: 7 additions & 2 deletions src/obsidian/Frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ async function _setFrontmatterKeys(
) {
let content = await app.vault.read(file);
for (const [key, value] of Object.entries(records)) {
if (_getFrontmatterKey(file, key, app) !== value) {
content = _setFrontmatterKey(file, key, value, content);
if (_getFrontmatterKey(file, key as FrontmatterKey, app) !== value) {
content = _setFrontmatterKey(
file,
key as FrontmatterKey,
value,
content
);
}
}
await app.vault.modify(file, content);
Expand Down
40 changes: 34 additions & 6 deletions src/ui/QuickShareSideView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import ActiveCacheFile from "src/lib/stores/ActiveCacheFile";
let data: QuickShareDataList;
$: data = $CacheStore;
let filteredData: QuickShareDataList;
function hasExpired(data: QuickShareData) {
const expiration = moment(data.expire_datetime);
Expand Down Expand Up @@ -87,6 +86,11 @@
$PluginStore.shareNote(file);
}
$: data = $CacheStore;
$: filteredData = data?.filter(
(d) => !deletedFromServer(d) && !(deletedFromVault(d) && hasExpired(d))
);
onMount(() => {
// Force a rerender every 30 seconds to update rendered timestamps
const timer = window.setInterval(() => {
Expand Down Expand Up @@ -154,10 +158,13 @@
<div id="history">
<div class="history-header">Recently shared</div>
<div class="history-list">
{#each data as item}
{#each filteredData as item}
<!-- svelte-ignore a11y-unknown-aria-attribute -->
<div
aria-label="Click to open note"
aria-label={!deletedFromVault(item)
? `Click to open note`
: undefined}
aria-label-position="left"
class="history-item
{hasExpired(item) && 'history-item--expired'}
{deletedFromServer(item) && 'history-item--deleted-server'}
Expand All @@ -166,9 +173,18 @@
<div class="item-row">
<div
class="item-description"
on:click={() => onOpenNote(item.fileId)}
on:click={() =>
!deletedFromVault(item) &&
onOpenNote(item.fileId)}
>
<div class="item-name">{item.basename}</div>
<div class="item-name">
{item.basename}
{#if deletedFromVault(item)}
<span class="deleted-text">
(Deleted from vault)
</span>
{/if}
</div>
<div class="item-sub">
{getSubText(item)}
</div>
Expand Down Expand Up @@ -274,6 +290,12 @@
color: var(--text-faint);
}
.item-deleted-vault {
font-size: 85%;
color: var(--text-error);
margin-top: 4px;
}
&:hover {
background-color: var(--nav-item-background-hover);
font-weight: var(--nav-item-weight-hover);
Expand All @@ -291,6 +313,12 @@
color: var(--text-faint);
}
}
&--deleted-vault {
.item-name {
color: var(--text-error);
}
}
}
}
}
Expand Down

0 comments on commit 5921345

Please sign in to comment.