Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix edit import/exports #3698

Merged
merged 3 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/ui/options/components/edit-options/edited-tracks.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { t } from '@/util/i18n';
import { For, Setter, createResource } from 'solid-js';
import { For, Setter, createResource, createSignal } from 'solid-js';
import * as Options from '@/core/storage/options';
import * as BrowserStorage from '@/core/storage/browser-storage';
import styles from '../components.module.scss';
Expand Down Expand Up @@ -147,22 +147,16 @@ async function downloadEdits() {
* Button that allows the user to upload a .json file and get edits from it.
*/
function ImportEdits() {
const [ref, setRef] = createSignal<HTMLInputElement>();
return (
<button
class={styles.editButton}
onClick={() =>
(
document.querySelector('#import-edits') as HTMLInputElement
)?.click()
}
>
<button class={styles.editButton} onClick={() => ref()?.click()}>
<Download />
{t('optionsImportEdited')}
<input
hidden={true}
ref={setRef}
type="file"
accept=".json"
id="import-edits"
onChange={pushEdits}
/>
</button>
Expand All @@ -184,10 +178,12 @@ function pushEdits(
reader.addEventListener('load', async (e) => {
const edits = JSON.parse(e.target?.result as string);
const oldEdits = await localCache.get();
localCache.set({
const newEdits = {
...oldEdits,
...edits,
});
};
localCache.set(newEdits);
mutate(newEdits);
});
reader.readAsText(file);
}
25 changes: 9 additions & 16 deletions src/ui/options/components/edit-options/regex-edits.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { t } from '@/util/i18n';
import { For, Setter, createResource } from 'solid-js';
import { For, Setter, createResource, createSignal } from 'solid-js';
import * as BrowserStorage from '@/core/storage/browser-storage';
import styles from '../components.module.scss';
import Download from '@suid/icons-material/DownloadOutlined';
Expand Down Expand Up @@ -167,36 +167,27 @@ function ExportEdits() {
async function downloadEdits() {
const edits = await regexEdits.get();
if (!edits) return;
const blob = new Blob([JSON.stringify(edits)], {
type: 'application/json',
});
const url = URL.createObjectURL(blob);
const data = `data:text/json;base64,${btoa(JSON.stringify(edits))}`;
const a = document.createElement('a');
a.href = url;
a.download = 'local-cache.json';
a.href = data;
a.click();
}

/**
* Button that allows the user to upload a .json file and get edits from it.
*/
function ImportEdits() {
const [ref, setRef] = createSignal<HTMLInputElement>();
return (
<button
class={styles.editButton}
onClick={() =>
(
document.querySelector('#import-edits') as HTMLInputElement
)?.click()
}
>
<button class={styles.editButton} onClick={() => ref()?.click()}>
<Download />
{t('optionsImportEdited')}
<input
hidden={true}
ref={setRef}
type="file"
accept=".json"
id="import-edits"
onChange={pushEdits}
/>
</button>
Expand All @@ -218,7 +209,9 @@ function pushEdits(
reader.addEventListener('load', async (e) => {
const edits = JSON.parse(e.target?.result as string);
const oldEdits = await regexEdits.get();
regexEdits.set([...(oldEdits ?? []), ...edits]);
const newEdits = [...(oldEdits ?? []), ...edits];
regexEdits.set(newEdits);
mutate(newEdits);
});
reader.readAsText(file);
}