Skip to content

Commit

Permalink
feat: #177 custom sup name (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyChen777 authored May 23, 2024
1 parent efdbc5a commit 4f9f222
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app/main/services/contextmenu-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export interface IContextMenuServiceState {
sidebarContextMenuColorClicked: { data: string; type: string; color: string };
sidebarContextMenuDeleteClicked: { data: string; type: string };
supContextMenuDeleteClicked: string;
supContextMenuRenameClicked: string;
thumbnailContextMenuReplaceClicked: number;
thumbnailContextMenuRefreshClicked: number;
linkToFolderClicked: string;
Expand Down Expand Up @@ -127,6 +128,7 @@ export class ContextMenuService extends Eventable<IContextMenuServiceState> {
sidebarContextMenuColorClicked: { data: "", type: "", color: "" },
sidebarContextMenuDeleteClicked: { data: "", type: "" },
supContextMenuDeleteClicked: "",
supContextMenuRenameClicked: "",
thumbnailContextMenuReplaceClicked: 0,
thumbnailContextMenuRefreshClicked: 0,
linkToFolderClicked: "",
Expand Down Expand Up @@ -548,6 +550,12 @@ export class ContextMenuService extends Eventable<IContextMenuServiceState> {
this.fire({ supContextMenuDeleteClicked: fileURL });
},
},
{
label: this._locales.t("menu.edit"),
click: () => {
this.fire({ supContextMenuRenameClicked: fileURL });
},
}
];
const menu = Menu.buildFromTemplate(template);
menu.popup();
Expand Down
16 changes: 13 additions & 3 deletions app/renderer/services/file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,20 @@ export class FileService extends Eventable<IFileServiceState> {
if (moveSups) {
const movedSupURLs: string[] = [];
for (let i = 0; i < paperEntity.supURLs.length; i++) {

let customDisplayName = "";
let realSupURL = paperEntity.supURLs[i];
if (paperEntity.supURLs[i].split(":::").length > 1) {
// The URL contains custom display name
customDisplayName = paperEntity.supURLs[i].split(":::")[0] + ":::"
realSupURL = paperEntity.supURLs[i].split(":::").slice(1).join(":::");
}

const movedSupFileName = await backend.moveFile(
paperEntity.supURLs[i],
realSupURL,
`${formatedFilename}_sup${i}${path.extname(paperEntity.supURLs[i])}`
);
movedSupURLs.push(movedSupFileName);
movedSupURLs.push(`${customDisplayName}${movedSupFileName}`);
}
paperEntity.supURLs = movedSupURLs;
}
Expand Down Expand Up @@ -313,7 +322,8 @@ export class FileService extends Eventable<IFileServiceState> {
async removeFile(url: string): Promise<void> {
const backend = await this.backend();

return await backend.removeFile(url);
const realURL = url.split(":::").pop() as string;
return await backend.removeFile(realURL);
}

/**
Expand Down
29 changes: 28 additions & 1 deletion app/renderer/services/paper-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,14 +569,41 @@ export class PaperService extends Eventable<IPaperServiceState> {
true,
"Entity"
);
await this._fileService.removeFile(url);
const realURL = url.split(":::").pop() as string;
await this._fileService.removeFile(realURL);
paperEntity.supURLs = paperEntity.supURLs.filter(
(supUrl) => !url.endsWith(supUrl)
);

await this.update([paperEntity], false, true);
}

/**
* Set a custom display name of a supplementary file.
*/
@processing(ProcessingKey.General)
@errorcatching(
"Failed to set custom display name of a supplementary file.",
true,
"PaperService"
)
async setSupDisplayName(paperEntity: PaperEntity, url: string, name: string) {
if (this._databaseCore.getState("dbInitializing")) {
return;
}

paperEntity.supURLs = paperEntity.supURLs.map((supURL) => {
if (supURL === url) {
const realSupURL = supURL.split(":::").pop();
return `${name}:::${realSupURL}`
} else {
return supURL;
}
});

await this.update([paperEntity], false, true);
}

/**
* Create paper entity from file URLs.
* @param urlList - The list of URLs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ onMounted(() => {
class="rounded-md bg-neutral-200 dark:bg-neutral-700 flex h-10 px-3 grow"
>
<input
class="text-xs bg-transparent focus:outline-none dark:text-neutral-300"
class="text-xs bg-transparent focus:outline-none dark:text-neutral-300 w-full"
type="text"
:placeholder="$t('smartfilter.value')"
v-model="selectedValue"
Expand Down
37 changes: 33 additions & 4 deletions app/renderer/ui/main-view/detail-view/components/supplementary.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
<script setup lang="ts">
const props = defineProps({
sups: Object as () => Array<string>,
editingSup: String,
});
const getExtension = (sup: string) => {
const getDisplayName = (sup: string) => {
if (sup.startsWith("http")) {
return "HTTP";
} else {
if (sup.split(":::").length > 1) {
return sup.split(":::")[0];
}
const ext = sup.split(".").pop();
return ext?.toUpperCase() ?? "SUP";
}
};
const onClick = async (url: string) => {
fileService.open(await fileService.access(url, true));
const realURL = url.split(":::").pop() ?? url;
fileService.open(await fileService.access(realURL, true));
};
const onRightClicked = (event: MouseEvent, url: string) => {
PLMainAPI.contextMenuService.showSupMenu(url);
};
const emits = defineEmits(["update:sup-display-name"]);
const onSubmitDisplayName = (event: Event) => {
event.preventDefault();
event.stopPropagation();
const customName = (event.target as HTMLInputElement).value;
if (customName === "") {
return;
}
emits("update:sup-display-name", customName);
};
</script>

<template>
Expand All @@ -29,9 +49,18 @@ const onRightClicked = (event: MouseEvent, url: string) => {
@click="onClick(sup)"
@contextmenu="(e: MouseEvent) => onRightClicked(e, sup)"
>
<div class="text-xxs my-auto">
{{ getExtension(sup) }}
<div class="text-xxs my-auto" v-if="sup !== editingSup">
{{ getDisplayName(sup) }}
</div>
<input
v-else
class="my-auto text-xxs bg-transparent grow whitespace-nowrap border-2 rounded-md px-1 border-accentlight dark:border-accentdark truncate"
type="text"
autofocus
:value="getDisplayName(editingSup)"
@click.stop
@keydown.enter="onSubmitDisplayName"
/>
</div>
</div>
</template>
20 changes: 19 additions & 1 deletion app/renderer/ui/main-view/detail-view/paper-detail-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ disposable(
)
);
const renamingSup = ref("");
const onRenameSupClicked = (url: string) => {
renamingSup.value = url;
};
disposable(
PLMainAPI.contextMenuService.on(
"supContextMenuRenameClicked",
(newValue: { value: string }) => {
onRenameSupClicked(newValue.value);
}
)
);
const onSupDisplayNameUpdated = (customName: string) => {
paperService.setSupDisplayName(props.entity, renamingSup.value, customName);
renamingSup.value = "";
};
const dragAreaOpacity = ref(0);
const dragCount = ref(0);
const mainFileDragAreaHovered = ref(false);
Expand Down Expand Up @@ -320,7 +338,7 @@ onUpdated(() => {
:title="$t('mainview.supplementaries')"
v-if="entity.supURLs.length > 0"
>
<Supplementary :sups="entity.supURLs" />
<Supplementary :sups="entity.supURLs" :editingSup="renamingSup" @update:sup-display-name="onSupDisplayNameUpdated" />
</Section>
<Markdown :title="'Markdown'" :sups="entity.supURLs" />

Expand Down
6 changes: 3 additions & 3 deletions app/repositories/db-repository/paper-entity-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class PaperEntityRepository extends Eventable<IPaperEntityRepositoryState
);
}
});

if (object) {
if (!allowUpdate) {
return false;
Expand All @@ -231,11 +231,11 @@ export class PaperEntityRepository extends Eventable<IPaperEntityRepositoryState
object.doi = paperEntity.doi;
object.arxiv = paperEntity.arxiv;
object.mainURL = paperEntity.mainURL;
object.supURLs = paperEntity.supURLs;
object.supURLs = Array.from(paperEntity.supURLs);
object.rating = paperEntity.rating;
object.flag = paperEntity.flag;
object.note = paperEntity.note;
object.codes = paperEntity.codes;
object.codes = Array.from(paperEntity.codes);
object.volume = paperEntity.volume;
object.number = paperEntity.number;
object.pages = paperEntity.pages;
Expand Down

0 comments on commit 4f9f222

Please sign in to comment.