Various config persistency fixes
parent
6d8844bd43
commit
d2416d829d
|
|
@ -33,7 +33,6 @@
|
||||||
"neo-scraper": "^0.5.8",
|
"neo-scraper": "^0.5.8",
|
||||||
"normalize.css": "^8.0.1",
|
"normalize.css": "^8.0.1",
|
||||||
"pinia": "^2.0.36",
|
"pinia": "^2.0.36",
|
||||||
"pinia-plugin-persistedstate": "^3.1.0",
|
|
||||||
"vue-router": "4"
|
"vue-router": "4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@ specifiers:
|
||||||
normalize.css: ^8.0.1
|
normalize.css: ^8.0.1
|
||||||
npm-run-all: ^4.1.5
|
npm-run-all: ^4.1.5
|
||||||
pinia: ^2.0.36
|
pinia: ^2.0.36
|
||||||
pinia-plugin-persistedstate: ^3.1.0
|
|
||||||
rimraf: ^3.0.2
|
rimraf: ^3.0.2
|
||||||
sass: ^1.57.1
|
sass: ^1.57.1
|
||||||
typescript: ^4.9.3
|
typescript: ^4.9.3
|
||||||
|
|
@ -54,7 +53,6 @@ dependencies:
|
||||||
neo-scraper: 0.5.8
|
neo-scraper: 0.5.8
|
||||||
normalize.css: 8.0.1
|
normalize.css: 8.0.1
|
||||||
pinia: 2.0.36_hmuptsblhheur2tugfgucj7gc4
|
pinia: 2.0.36_hmuptsblhheur2tugfgucj7gc4
|
||||||
pinia-plugin-persistedstate: 3.1.0_pinia@2.0.36
|
|
||||||
vue-router: 4.1.6_vue@3.2.47
|
vue-router: 4.1.6_vue@3.2.47
|
||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
|
|
@ -4974,14 +4972,6 @@ packages:
|
||||||
engines: {node: '>=4'}
|
engines: {node: '>=4'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/pinia-plugin-persistedstate/3.1.0_pinia@2.0.36:
|
|
||||||
resolution: {integrity: sha512-8UN+vYMEPBdgNLwceY08mi5olI0wkYaEb8b6hD6xW7SnBRuPydWHlEhZvUWgNb/ibuf4PvufpvtS+dmhYjJQOw==}
|
|
||||||
peerDependencies:
|
|
||||||
pinia: ^2.0.0
|
|
||||||
dependencies:
|
|
||||||
pinia: 2.0.36_hmuptsblhheur2tugfgucj7gc4
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/pinia/2.0.36_hmuptsblhheur2tugfgucj7gc4:
|
/pinia/2.0.36_hmuptsblhheur2tugfgucj7gc4:
|
||||||
resolution: {integrity: sha512-4UKApwjlmJH+VuHKgA+zQMddcCb3ezYnyewQ9NVrsDqZ/j9dMv5+rh+1r48whKNdpFkZAWVxhBp5ewYaYX9JcQ==}
|
resolution: {integrity: sha512-4UKApwjlmJH+VuHKgA+zQMddcCb3ezYnyewQ9NVrsDqZ/j9dMv5+rh+1r48whKNdpFkZAWVxhBp5ewYaYX9JcQ==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,7 @@ export default class SzurubooruApi {
|
||||||
|
|
||||||
async createPost(post: ScrapedPostDetails, contentToken?: string): Promise<Post> {
|
async createPost(post: ScrapedPostDetails, contentToken?: string): Promise<Post> {
|
||||||
const obj = <any>{
|
const obj = <any>{
|
||||||
tags: post.tags.map((x) => x.name),
|
tags: post.tags.map((x) => x.names[0]),
|
||||||
safety: post.rating,
|
safety: post.rating,
|
||||||
source: post.source,
|
source: post.source,
|
||||||
notes: post.notes,
|
notes: post.notes,
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ async function uploadPost(data: PostUploadCommandData) {
|
||||||
const tagsWithCategory = data.post.tags.filter((x) => x.category);
|
const tagsWithCategory = data.post.tags.filter((x) => x.category);
|
||||||
const unsetCategoryTags = createdPost.tags
|
const unsetCategoryTags = createdPost.tags
|
||||||
.filter((x) => x.category == "default")
|
.filter((x) => x.category == "default")
|
||||||
.filter((x) => tagsWithCategory.some((y) => x.names.includes(y.name)));
|
.filter((x) => tagsWithCategory.some((y) => x.names.includes(y.names[0])));
|
||||||
|
|
||||||
if (unsetCategoryTags.length != 0) {
|
if (unsetCategoryTags.length != 0) {
|
||||||
info.updateTagsState = {
|
info.updateTagsState = {
|
||||||
|
|
@ -64,7 +64,7 @@ async function uploadPost(data: PostUploadCommandData) {
|
||||||
info.updateTagsState.current = parseInt(i);
|
info.updateTagsState.current = parseInt(i);
|
||||||
pushInfo();
|
pushInfo();
|
||||||
|
|
||||||
const wantedCategory = tagsWithCategory.find((x) => tags[i].names.includes(x.name))?.category;
|
const wantedCategory = tagsWithCategory.find((x) => tags[i].names.includes(x.names[0]))?.category;
|
||||||
if (wantedCategory) {
|
if (wantedCategory) {
|
||||||
if (existingCategories.some((x) => x.name == wantedCategory)) {
|
if (existingCategories.some((x) => x.name == wantedCategory)) {
|
||||||
tags[i].category = wantedCategory;
|
tags[i].category = wantedCategory;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { storage } from "webextension-polyfill";
|
||||||
|
import type { MaybeRef, RemovableRef, StorageLikeAsync, UseStorageAsyncOptions } from "@vueuse/core";
|
||||||
|
import { useStorageAsync } from "@vueuse/core";
|
||||||
|
|
||||||
|
const storageLocal: StorageLikeAsync = {
|
||||||
|
removeItem(key: string) {
|
||||||
|
return storage.local.remove(key);
|
||||||
|
},
|
||||||
|
|
||||||
|
setItem(key: string, value: string) {
|
||||||
|
console.log("setItem");
|
||||||
|
console.dir(value);
|
||||||
|
return storage.local.set({ [key]: value });
|
||||||
|
},
|
||||||
|
|
||||||
|
async getItem(key: string) {
|
||||||
|
console.log("getItem");
|
||||||
|
const item = (await storage.local.get(key))[key];
|
||||||
|
console.dir(item);
|
||||||
|
return item;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useStorageLocal = <T>(
|
||||||
|
key: string,
|
||||||
|
initialValue: MaybeRef<T>,
|
||||||
|
options?: UseStorageAsyncOptions<T>
|
||||||
|
): RemovableRef<T> => useStorageAsync(key, initialValue, storageLocal, options);
|
||||||
|
|
@ -3,11 +3,9 @@ import { library } from "@fortawesome/fontawesome-svg-core";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
|
||||||
import { faCog, faChevronDown, faChevronUp, faChevronLeft } from "@fortawesome/free-solid-svg-icons";
|
import { faCog, faChevronDown, faChevronUp, faChevronLeft } from "@fortawesome/free-solid-svg-icons";
|
||||||
import { createPinia } from "pinia";
|
import { createPinia } from "pinia";
|
||||||
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
|
|
||||||
|
|
||||||
export function setupPinia(app: App) {
|
export function setupPinia(app: App) {
|
||||||
const pinia = createPinia();
|
const pinia = createPinia();
|
||||||
pinia.use(piniaPluginPersistedstate);
|
|
||||||
app.use(pinia);
|
app.use(pinia);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,24 +2,18 @@
|
||||||
import { useColorMode } from "@vueuse/core";
|
import { useColorMode } from "@vueuse/core";
|
||||||
import SzuruWrapper from "~/api";
|
import SzuruWrapper from "~/api";
|
||||||
import { SzuruSiteConfig } from "~/config";
|
import { SzuruSiteConfig } from "~/config";
|
||||||
import { useConfigStore } from "~/stores";
|
import { cfg } from "~/stores";
|
||||||
import { getErrorMessage } from "~/utils";
|
import { getErrorMessage } from "~/utils";
|
||||||
|
|
||||||
let autoSearchSimilar = ref(false);
|
let statusText = ref("Version " + browser.runtime.getManifest().version);
|
||||||
let loadTagCounts = ref(false);
|
|
||||||
let addPageUrlToSource = ref(false);
|
|
||||||
let statusText = ref("");
|
|
||||||
let statusType = ref("status-quiet");
|
let statusType = ref("status-quiet");
|
||||||
let sites = reactive<SzuruSiteConfig[]>([]);
|
|
||||||
let selectedSiteId = ref<string | undefined>(undefined);
|
|
||||||
const selectedSite = computed(() => {
|
const selectedSite = computed(() => {
|
||||||
if (selectedSiteId.value) {
|
if (cfg.value.selectedSiteId) {
|
||||||
return sites.find((x) => x.id == selectedSiteId.value);
|
return cfg.value.sites.find((x) => x.id == cfg.value.selectedSiteId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const configStore = useConfigStore();
|
|
||||||
|
|
||||||
type StatusType = "success" | "error" | "quiet";
|
type StatusType = "success" | "error" | "quiet";
|
||||||
|
|
||||||
async function testConnection() {
|
async function testConnection() {
|
||||||
|
|
@ -48,16 +42,6 @@ async function testConnection() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveSettings() {
|
|
||||||
configStore.sites = sites;
|
|
||||||
configStore.autoSearchSimilar = autoSearchSimilar.value;
|
|
||||||
configStore.loadTagCounts = loadTagCounts.value;
|
|
||||||
configStore.addPageUrlToSource = addPageUrlToSource.value;
|
|
||||||
configStore.selectedSiteId = selectedSiteId.value;
|
|
||||||
|
|
||||||
setStatus("Settings successfully saved");
|
|
||||||
}
|
|
||||||
|
|
||||||
function setStatus(text: string, type: StatusType = "success") {
|
function setStatus(text: string, type: StatusType = "success") {
|
||||||
statusText.value = text;
|
statusText.value = text;
|
||||||
statusType.value = "status-" + type;
|
statusType.value = "status-" + type;
|
||||||
|
|
@ -65,31 +49,22 @@ function setStatus(text: string, type: StatusType = "success") {
|
||||||
|
|
||||||
function addSite() {
|
function addSite() {
|
||||||
const site = new SzuruSiteConfig();
|
const site = new SzuruSiteConfig();
|
||||||
sites.push(site);
|
cfg.value.sites.push(site);
|
||||||
selectedSiteId.value = site.id;
|
cfg.value.selectedSiteId = site.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeSelectedSite() {
|
function removeSelectedSite() {
|
||||||
if (selectedSite.value) {
|
if (selectedSite.value) {
|
||||||
const idx = sites.indexOf(selectedSite.value);
|
const idx = cfg.value.sites.indexOf(selectedSite.value);
|
||||||
sites.splice(idx, 1);
|
cfg.value.sites.splice(idx, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select first site in list, if it exists.
|
||||||
|
if (cfg.value.sites.length > 0) {
|
||||||
|
cfg.value.selectedSiteId = cfg.value.sites[0].id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
statusText.value = "Version " + browser.runtime.getManifest().version;
|
|
||||||
|
|
||||||
for (let site of configStore.sites) {
|
|
||||||
// This somehow makes `site` reactive.
|
|
||||||
sites.push(site);
|
|
||||||
}
|
|
||||||
|
|
||||||
autoSearchSimilar.value = configStore.autoSearchSimilar;
|
|
||||||
loadTagCounts.value = configStore.loadTagCounts;
|
|
||||||
addPageUrlToSource.value = configStore.addPageUrlToSource;
|
|
||||||
selectedSiteId.value = configStore.selectedSiteId;
|
|
||||||
});
|
|
||||||
|
|
||||||
let mode = useColorMode({ emitAuto: true });
|
let mode = useColorMode({ emitAuto: true });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
@ -105,8 +80,8 @@ let mode = useColorMode({ emitAuto: true });
|
||||||
|
|
||||||
<!-- This isn't perfectly responsive for very small devices. -->
|
<!-- This isn't perfectly responsive for very small devices. -->
|
||||||
<div class="fit flex gap-1 flex-fit">
|
<div class="fit flex gap-1 flex-fit">
|
||||||
<select v-model="selectedSiteId">
|
<select v-model="cfg.selectedSiteId">
|
||||||
<option v-for="site in sites" :key="site.id" :value="site.id">
|
<option v-for="site in cfg.sites" :key="site.id" :value="site.id">
|
||||||
{{ site.username }} @ {{ site.domain }}
|
{{ site.username }} @ {{ site.domain }}
|
||||||
</option>
|
</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
@ -154,7 +129,7 @@ let mode = useColorMode({ emitAuto: true });
|
||||||
|
|
||||||
<div class="settings-block">
|
<div class="settings-block">
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" v-model="autoSearchSimilar" />
|
<input type="checkbox" v-model="cfg.autoSearchSimilar" />
|
||||||
Automatically search for similar posts
|
Automatically search for similar posts
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
|
|
@ -165,7 +140,7 @@ let mode = useColorMode({ emitAuto: true });
|
||||||
|
|
||||||
<div class="settings-block">
|
<div class="settings-block">
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" v-model="addPageUrlToSource" />
|
<input type="checkbox" v-model="cfg.addPageUrlToSource" />
|
||||||
Add page URL to the source list
|
Add page URL to the source list
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
|
|
@ -177,7 +152,7 @@ let mode = useColorMode({ emitAuto: true });
|
||||||
|
|
||||||
<div class="settings-block">
|
<div class="settings-block">
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" v-model="loadTagCounts" />
|
<input type="checkbox" v-model="cfg.loadTagCounts" />
|
||||||
Show how often a tag is used in the selected szurubooru instance
|
Show how often a tag is used in the selected szurubooru instance
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
|
|
@ -192,7 +167,7 @@ let mode = useColorMode({ emitAuto: true });
|
||||||
|
|
||||||
<div class="flex gap-1">
|
<div class="flex gap-1">
|
||||||
<button @click="testConnection">Test connection</button>
|
<button @click="testConnection">Test connection</button>
|
||||||
<button @click="saveSettings" class="primary">Save settings</button>
|
<!-- <button @click="saveSettings" class="primary">Save settings</button> -->
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,14 @@ import { isEqual, cloneDeep } from "lodash";
|
||||||
import SzurubooruApi from "~/api";
|
import SzurubooruApi from "~/api";
|
||||||
import { Post, UpdatePostRequest } from "~/api/models";
|
import { Post, UpdatePostRequest } from "~/api/models";
|
||||||
import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models";
|
import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models";
|
||||||
import { useConfigStore, useMergeStore, usePopupStore } from "~/stores";
|
import { cfg, useMergeStore, usePopupStore } from "~/stores";
|
||||||
import { emptyPost, getUrl } from "~/utils";
|
import { emptyPost, getUrl } from "~/utils";
|
||||||
|
|
||||||
const props = defineProps(["siteId", "postId"]);
|
const props = defineProps(["siteId", "postId"]);
|
||||||
const cfg = useConfigStore();
|
|
||||||
const merge = useMergeStore();
|
const merge = useMergeStore();
|
||||||
const pop = usePopupStore();
|
const pop = usePopupStore();
|
||||||
|
|
||||||
const szuruConfig = cfg.sites.find((x) => x.id == props.siteId)!;
|
const szuruConfig = cloneDeep(cfg.value.sites.find((x) => x.id == props.siteId))!;
|
||||||
const szuru = SzurubooruApi.createFromConfig(szuruConfig);
|
const szuru = SzurubooruApi.createFromConfig(szuruConfig);
|
||||||
|
|
||||||
// Used for the update post request.
|
// Used for the update post request.
|
||||||
|
|
@ -54,7 +53,7 @@ function addTag(tag: TagDetails) {
|
||||||
tagsToAdd.push(tag);
|
tagsToAdd.push(tag);
|
||||||
|
|
||||||
// Add implications for the tag
|
// Add implications for the tag
|
||||||
if (cfg.addTagImplications) {
|
if (cfg.value.addTagImplications) {
|
||||||
tagsToAdd.push(...tag.implications);
|
tagsToAdd.push(...tag.implications);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -162,7 +161,7 @@ watch(
|
||||||
);
|
);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => cfg.merge.addMissingTags,
|
() => cfg.value.merge.addMissingTags,
|
||||||
(value) => {
|
(value) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
addMissingTags();
|
addMissingTags();
|
||||||
|
|
@ -173,7 +172,7 @@ watch(
|
||||||
);
|
);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => cfg.merge.appendSource,
|
() => cfg.value.merge.appendSource,
|
||||||
(value) => {
|
(value) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
appendSources();
|
appendSources();
|
||||||
|
|
@ -184,7 +183,7 @@ watch(
|
||||||
);
|
);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => cfg.merge.mergeSafety,
|
() => cfg.value.merge.mergeSafety,
|
||||||
(value) => {
|
(value) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
mergeSafety();
|
mergeSafety();
|
||||||
|
|
@ -198,9 +197,9 @@ onMounted(async () => {
|
||||||
existingPostRo = await szuru?.getPost(props.postId);
|
existingPostRo = await szuru?.getPost(props.postId);
|
||||||
if (existingPostRo) {
|
if (existingPostRo) {
|
||||||
post.value = cloneDeep(existingPostRo);
|
post.value = cloneDeep(existingPostRo);
|
||||||
if (cfg.merge.appendSource) appendSources();
|
if (cfg.value.merge.appendSource) appendSources();
|
||||||
if (cfg.merge.addMissingTags) addMissingTags();
|
if (cfg.value.merge.addMissingTags) addMissingTags();
|
||||||
if (cfg.merge.mergeSafety) mergeSafety();
|
if (cfg.value.merge.mergeSafety) mergeSafety();
|
||||||
|
|
||||||
if (scrapedPost?.resolution) {
|
if (scrapedPost?.resolution) {
|
||||||
// Set imageToKeep depending on which image has the largest resolution.
|
// Set imageToKeep depending on which image has the largest resolution.
|
||||||
|
|
@ -257,6 +256,29 @@ onMounted(async () => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<PopupSection header="Changes">
|
||||||
|
<div class="section-row">
|
||||||
|
<ul class="compact-tags">
|
||||||
|
<li v-show="tagsToAdd.length != 0 || tagsRemoved != 0">
|
||||||
|
Tags: <span class="success" v-show="tagsToAdd.length != 0">+{{ tagsToAdd.length }}</span>
|
||||||
|
<span class="danger" v-show="tagsRemoved != 0">-{{ tagsRemoved }}</span>
|
||||||
|
</li>
|
||||||
|
<li v-show="sourcesAdded != 0 || sourcesRemoved != 0">
|
||||||
|
Sources: <span class="success" v-show="sourcesAdded != 0">+2</span>
|
||||||
|
<span class="danger" v-show="sourcesRemoved != 0">-1</span>
|
||||||
|
</li>
|
||||||
|
<li v-show="existingPostRo?.safety != post.safety">
|
||||||
|
Safety: <s class="danger">{{ existingPostRo?.safety }}</s> <span class="success">{{ post.safety }}</span>
|
||||||
|
</li>
|
||||||
|
<li v-show="imageToKeep == 'new'">
|
||||||
|
<span class="success">Updating post content</span>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<!-- TODO: Add "No changes" text. -->
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</PopupSection>
|
||||||
|
|
||||||
<PopupSection header="Basic info">
|
<PopupSection header="Basic info">
|
||||||
<div class="section-row">
|
<div class="section-row">
|
||||||
<span class="section-label">Safety</span>
|
<span class="section-label">Safety</span>
|
||||||
|
|
@ -349,27 +371,6 @@ onMounted(async () => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</PopupSection>
|
</PopupSection>
|
||||||
|
|
||||||
<PopupSection header="Changes">
|
|
||||||
<div class="section-row">
|
|
||||||
<ul class="compact-tags">
|
|
||||||
<li v-show="tagsToAdd.length != 0 || tagsRemoved != 0">
|
|
||||||
Tags: <span class="success" v-show="tagsToAdd.length != 0">+{{ tagsToAdd.length }}</span>
|
|
||||||
<span class="danger" v-show="tagsRemoved != 0">-{{ tagsRemoved }}</span>
|
|
||||||
</li>
|
|
||||||
<li v-show="sourcesAdded != 0 || sourcesRemoved != 0">
|
|
||||||
Sources: <span class="success" v-show="sourcesAdded != 0">+2</span>
|
|
||||||
<span class="danger" v-show="sourcesRemoved != 0">-1</span>
|
|
||||||
</li>
|
|
||||||
<li v-show="existingPostRo?.safety != post.safety">
|
|
||||||
Safety: <s class="danger">{{ existingPostRo?.safety }}</s> <span class="success">{{ post.safety }}</span>
|
|
||||||
</li>
|
|
||||||
<li v-show="imageToKeep == 'new'">
|
|
||||||
<span class="success">Updating post content</span>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useDark } from "@vueuse/core";
|
import { useDark } from "@vueuse/core";
|
||||||
|
import { cloneDeep } from "lodash";
|
||||||
import { ScrapeResults } from "neo-scraper";
|
import { ScrapeResults } from "neo-scraper";
|
||||||
import { getUrl, encodeTagName, getErrorMessage } from "~/utils";
|
import { getUrl, encodeTagName, getErrorMessage } from "~/utils";
|
||||||
import {
|
import {
|
||||||
|
|
@ -14,18 +15,29 @@ import {
|
||||||
import { ImageSearchResult } from "~/api/models";
|
import { ImageSearchResult } from "~/api/models";
|
||||||
import { isMobile } from "~/env";
|
import { isMobile } from "~/env";
|
||||||
import { DeepReadonly } from "vue";
|
import { DeepReadonly } from "vue";
|
||||||
import { useConfigStore, usePopupStore } from "~/stores";
|
import { cfg, usePopupStore } from "~/stores";
|
||||||
|
import { SzuruSiteConfig } from "~/config";
|
||||||
|
import SzurubooruApi from "~/api";
|
||||||
|
|
||||||
const pop = usePopupStore();
|
const pop = usePopupStore();
|
||||||
const cfg = useConfigStore();
|
|
||||||
const isSearchingForSimilarPosts = ref<number>(0);
|
const isSearchingForSimilarPosts = ref<number>(0);
|
||||||
|
|
||||||
|
const selectedSite = computed(() => {
|
||||||
|
if (cfg.value.selectedSiteId) {
|
||||||
|
return cfg.value.sites.find((x) => x.id == cfg.value.selectedSiteId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const szuru = computed(() => {
|
||||||
|
return selectedSite.value ? SzurubooruApi.createFromConfig(selectedSite.value) : undefined;
|
||||||
|
});
|
||||||
|
|
||||||
// (Reactive) shorthand for `main.selectedPost.instanceSpecificData.get(config.selectedSiteId)`.
|
// (Reactive) shorthand for `main.selectedPost.instanceSpecificData.get(config.selectedSiteId)`.
|
||||||
// This should only be used as if it were readonly, because the instanceId might change.
|
// This should only be used as if it were readonly, because the instanceId might change.
|
||||||
const instanceSpecificData = readonly(
|
const instanceSpecificData = readonly(
|
||||||
computed(() => {
|
computed(() => {
|
||||||
if (pop.selectedPost && cfg.selectedSiteId) {
|
if (pop.selectedPost && cfg.value.selectedSiteId) {
|
||||||
return pop.selectedPost.instanceSpecificData[cfg.selectedSiteId];
|
return pop.selectedPost.instanceSpecificData[cfg.value.selectedSiteId];
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
@ -34,7 +46,7 @@ watch(
|
||||||
() => pop.selectedPostId,
|
() => pop.selectedPostId,
|
||||||
(value) => {
|
(value) => {
|
||||||
// Call findSimilar if wanted.
|
// Call findSimilar if wanted.
|
||||||
if (cfg.autoSearchSimilar) {
|
if (cfg.value.autoSearchSimilar) {
|
||||||
let selectedPost = pop.posts.find((x) => x.id == value);
|
let selectedPost = pop.posts.find((x) => x.id == value);
|
||||||
if (selectedPost) findSimilar(selectedPost);
|
if (selectedPost) findSimilar(selectedPost);
|
||||||
}
|
}
|
||||||
|
|
@ -42,14 +54,14 @@ watch(
|
||||||
);
|
);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => cfg.selectedSiteId,
|
() => cfg.value.selectedSiteId,
|
||||||
async (value, oldValue) => {
|
async (value, oldValue) => {
|
||||||
// If changed
|
// If changed
|
||||||
if (value != oldValue) {
|
if (value != oldValue) {
|
||||||
// Update selectedSiteId in config
|
// Update selectedSiteId in config
|
||||||
cfg.selectedSiteId = value;
|
cfg.value.selectedSiteId = value;
|
||||||
// Call findSimilar if wanted.
|
// Call findSimilar if wanted.
|
||||||
if (cfg.autoSearchSimilar && pop.selectedPost) {
|
if (cfg.value.autoSearchSimilar && pop.selectedPost) {
|
||||||
findSimilar(pop.selectedPost);
|
findSimilar(pop.selectedPost);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -93,13 +105,13 @@ async function grabPost() {
|
||||||
// Add current pageUrl to the source if either
|
// Add current pageUrl to the source if either
|
||||||
// a. user has addPageUrlToSource set to true
|
// a. user has addPageUrlToSource set to true
|
||||||
// b. source is empty
|
// b. source is empty
|
||||||
if (cfg?.addPageUrlToSource || vm.source == "") {
|
if (cfg.value.addPageUrlToSource || vm.source == "") {
|
||||||
if (vm.source != "") vm.source += "\n";
|
if (vm.source != "") vm.source += "\n";
|
||||||
vm.source += vm.pageUrl;
|
vm.source += vm.pageUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize instanceSpecificData with an empty object.
|
// Initialize instanceSpecificData with an empty object.
|
||||||
for (const site of cfg.sites) {
|
for (const site of cfg.value.sites) {
|
||||||
vm.instanceSpecificData[site.id] = {};
|
vm.instanceSpecificData[site.id] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,7 +121,7 @@ async function grabPost() {
|
||||||
|
|
||||||
if (pop.posts.length > 0) {
|
if (pop.posts.length > 0) {
|
||||||
pop.selectedPostId = pop.posts[0].id;
|
pop.selectedPostId = pop.posts[0].id;
|
||||||
if (cfg?.loadTagCounts) {
|
if (cfg.value.loadTagCounts) {
|
||||||
loadTagCounts();
|
loadTagCounts();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -118,8 +130,8 @@ async function grabPost() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upload() {
|
async function upload() {
|
||||||
if (!cfg.selectedSiteId) return;
|
if (!cfg.value.selectedSiteId) return;
|
||||||
let instanceSpecificData = pop.selectedPost?.instanceSpecificData[cfg.selectedSiteId];
|
let instanceSpecificData = pop.selectedPost?.instanceSpecificData[cfg.value.selectedSiteId];
|
||||||
|
|
||||||
// Don't try to upload if an exact copy is already on the server.
|
// Don't try to upload if an exact copy is already on the server.
|
||||||
if (instanceSpecificData?.reverseSearchResult?.exactPostId) return;
|
if (instanceSpecificData?.reverseSearchResult?.exactPostId) return;
|
||||||
|
|
@ -128,11 +140,18 @@ async function upload() {
|
||||||
instanceSpecificData.uploadState = { state: "uploading" };
|
instanceSpecificData.uploadState = { state: "uploading" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const post: any = { ...pop.selectedPost };
|
try {
|
||||||
// @ts-expect-error ...
|
const post: any = cloneDeep(pop.selectedPost);
|
||||||
const cmdData = new PostUploadCommandData(post, cfg.selectedSite);
|
const cmdData = new PostUploadCommandData(post, <SzuruSiteConfig>cloneDeep(selectedSite.value));
|
||||||
const cmd = new BrowserCommand("upload_post", cmdData);
|
const cmd = new BrowserCommand("upload_post", cmdData);
|
||||||
await browser.runtime.sendMessage(cmd);
|
await browser.runtime.sendMessage(cmd);
|
||||||
|
} catch (ex: any) {
|
||||||
|
if (instanceSpecificData?.uploadState) {
|
||||||
|
instanceSpecificData.uploadState.state = "error";
|
||||||
|
instanceSpecificData.uploadState.error = ex.toString();
|
||||||
|
}
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolutionToString(resolution: [number, number]) {
|
function resolutionToString(resolution: [number, number]) {
|
||||||
|
|
@ -152,8 +171,8 @@ function removeTag(tag: TagDetails) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getActiveSitePostUrl(postId: number): string {
|
function getActiveSitePostUrl(postId: number): string {
|
||||||
if (!cfg.selectedSite) return "";
|
if (!selectedSite.value) return "";
|
||||||
return getUrl(cfg.selectedSite.domain, "post", postId.toString());
|
return getUrl(selectedSite.value.domain, "post", postId.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSimilarPosts(data?: DeepReadonly<SimpleImageSearchResult>): SimilarPostInfo[] {
|
function getSimilarPosts(data?: DeepReadonly<SimpleImageSearchResult>): SimilarPostInfo[] {
|
||||||
|
|
@ -179,7 +198,7 @@ function addTag(tag: TagDetails) {
|
||||||
pop.selectedPost.tags.push(tag);
|
pop.selectedPost.tags.push(tag);
|
||||||
|
|
||||||
// Add implications for the tag
|
// Add implications for the tag
|
||||||
if (cfg.addTagImplications) {
|
if (cfg.value.addTagImplications) {
|
||||||
pop.selectedPost.tags.push(...tag.implications);
|
pop.selectedPost.tags.push(...tag.implications);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -191,10 +210,11 @@ async function clickFindSimilar() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findSimilar(post: ScrapedPostDetails | undefined) {
|
async function findSimilar(post: ScrapedPostDetails | undefined) {
|
||||||
if (!post || !cfg.szuru || !cfg.selectedSiteId) return;
|
if (!post || !szuru.value || !cfg.value.selectedSiteId) return;
|
||||||
|
|
||||||
const selectedInstance = cfg.szuru; // Get current object, and not reactive.
|
// TODO: This code might not work.
|
||||||
let instanceSpecificData = post.instanceSpecificData[cfg.selectedSiteId];
|
const selectedInstance = toRaw(szuru.value); // Get current object, and not reactive.
|
||||||
|
let instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
|
||||||
|
|
||||||
if (!instanceSpecificData) {
|
if (!instanceSpecificData) {
|
||||||
console.error("instanceSpecificData is undefined. This should never happen!");
|
console.error("instanceSpecificData is undefined. This should never happen!");
|
||||||
|
|
@ -209,7 +229,7 @@ async function findSimilar(post: ScrapedPostDetails | undefined) {
|
||||||
try {
|
try {
|
||||||
let res: ImageSearchResult | undefined;
|
let res: ImageSearchResult | undefined;
|
||||||
|
|
||||||
if (cfg.useContentTokens) {
|
if (cfg.value.useContentTokens) {
|
||||||
let tmpRes = await selectedInstance.uploadTempFile(post.contentUrl);
|
let tmpRes = await selectedInstance.uploadTempFile(post.contentUrl);
|
||||||
// TODO: Error handling?
|
// TODO: Error handling?
|
||||||
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
|
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
|
||||||
|
|
@ -240,7 +260,7 @@ async function loadTagCounts() {
|
||||||
.map((x) => encodeTagName(x.name))
|
.map((x) => encodeTagName(x.name))
|
||||||
.join();
|
.join();
|
||||||
|
|
||||||
const resp = await cfg.szuru?.getTags(query);
|
const resp = await szuru.value?.getTags(query);
|
||||||
|
|
||||||
// Not the best code, but it works I guess?
|
// Not the best code, but it works I guess?
|
||||||
if (resp) {
|
if (resp) {
|
||||||
|
|
@ -283,10 +303,10 @@ useDark();
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<div v-if="cfg.sites.length == 0" class="bg-danger">
|
<div v-if="cfg.sites.length == 0" class="bg-danger">
|
||||||
<span>No szurubooru server configured!</span>
|
<span>No szurubooru servers configured!</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="!cfg.selectedSite" class="bg-danger">
|
<div v-if="!szuru && cfg.sites.length != 0" class="bg-danger">
|
||||||
<span>No target server selected!</span>
|
<span>No target server selected!</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -453,7 +473,7 @@ useDark();
|
||||||
|
|
||||||
<PopupSection header="Tags">
|
<PopupSection header="Tags">
|
||||||
<div class="section-row">
|
<div class="section-row">
|
||||||
<TagInput :szuru="cfg.szuru" @add-tag="addTag" />
|
<TagInput :szuru="szuru" @add-tag="addTag" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="section-row">
|
<div class="section-row">
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { SzuruSiteConfig } from "~/config";
|
import { SzuruSiteConfig } from "~/config";
|
||||||
import SzurubooruApi from "~/api";
|
|
||||||
import type { ScrapedPostDetails, SetPostUploadInfoData } from "~/models";
|
import type { ScrapedPostDetails, SetPostUploadInfoData } from "~/models";
|
||||||
|
import { useStorageLocal } from "~/composables/useStorageLocal";
|
||||||
|
|
||||||
export const useConfigStore = defineStore("config", {
|
export const cfg = useStorageLocal("config", {
|
||||||
state: () => ({
|
|
||||||
addPageUrlToSource: true,
|
addPageUrlToSource: true,
|
||||||
autoSearchSimilar: false,
|
autoSearchSimilar: false,
|
||||||
loadTagCounts: true,
|
loadTagCounts: true,
|
||||||
|
|
@ -20,26 +19,6 @@ export const useConfigStore = defineStore("config", {
|
||||||
appendSource: true,
|
appendSource: true,
|
||||||
mergeSafety: true,
|
mergeSafety: true,
|
||||||
},
|
},
|
||||||
}),
|
|
||||||
getters: {
|
|
||||||
selectedSite: (state) => {
|
|
||||||
return state.sites.find((x) => x.id == state.selectedSiteId);
|
|
||||||
},
|
|
||||||
szuru(): SzurubooruApi | undefined {
|
|
||||||
if (this.selectedSite) {
|
|
||||||
return SzurubooruApi.createFromConfig(this.selectedSite);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
createSzuruApiFromConfig(siteId: string) {
|
|
||||||
const cfg = this.sites.find((x) => x.id == siteId);
|
|
||||||
if (cfg) {
|
|
||||||
return SzurubooruApi.createFromConfig(cfg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
persist: true,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const usePopupStore = defineStore("popup", {
|
export const usePopupStore = defineStore("popup", {
|
||||||
|
|
|
||||||
|
|
@ -71,10 +71,6 @@ export default defineConfig(({ command }) => ({
|
||||||
outDir: r("extension/dist"),
|
outDir: r("extension/dist"),
|
||||||
emptyOutDir: false,
|
emptyOutDir: false,
|
||||||
sourcemap: isDev ? "inline" : false,
|
sourcemap: isDev ? "inline" : false,
|
||||||
// https://developer.chrome.com/docs/webstore/program_policies/#:~:text=Code%20Readability%20Requirements
|
|
||||||
terserOptions: {
|
|
||||||
mangle: false,
|
|
||||||
},
|
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
input: {
|
input: {
|
||||||
background: r("src/background/index.html"),
|
background: r("src/background/index.html"),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue