forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.svelte
More file actions
135 lines (121 loc) · 3.99 KB
/
Copy pathApp.svelte
File metadata and controls
135 lines (121 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<script>
import {onMount, tick} from "svelte";
import {blur} from "svelte/transition";
import {
items, log, notifications, settings, state
} from "./store.js";
import {t, waitLocale} from "svelte-i18n";
import Sidebar from "./Components/Sidebar/Sidebar.svelte";
import Editor from "./Components/Editor/Editor.svelte";
import Settings from "./Components/Settings.svelte";
import Notification from "./Components/Notification.svelte";
import logo from "../shared/img/logo.svg?raw";
const logger = [];
$: $log.some(item => {
if (!logger.includes(item)) {
// eslint-disable-next-line no-console -- not arbitrary console command
console[item.type](item.message);
logger.push(item);
}
});
// disables default cmd+s (save) and cmd+f (find) behavior
function preventKeyCommands(e) {
if (e.metaKey && (e.code === "KeyS" || e.code === "KeyF")) {
return e.preventDefault();
}
}
// app proportions can get messed up when opening/closing new tabs
async function windowResize() {
document.documentElement.style.height = "100vh";
// if tick is omitted, the style change won't apply
await tick();
document.documentElement.removeAttribute("style");
}
// currently inactive, but could be used to globally prevent auto text replacement in app
// function preventAutoTextReplacements(e) {
// if (e.inputType === "insertReplacementText" && e.data === ". ") {
// e.preventDefault();
// e.target.value += " ";
// }
// }
onMount(async () => {
await waitLocale();
log.add("Requesting initialization data", "info", false);
const initData = await browser.runtime.sendNativeMessage({name: "PAGE_INIT_DATA"});
if (initData.error) return console.error(initData.error);
await settings.init(initData);
state.add("items-loading");
state.remove("init");
log.add("Requesting all files in save location", "info", false);
const files = await browser.runtime.sendNativeMessage({name: "PAGE_ALL_FILES"});
if (files.error) return console.error(files.error);
items.set(files);
state.remove("items-loading");
});
// handle native app messages
const port = browser.runtime.connectNative();
port.onMessage.addListener(message => {
// console.info(message); // DEBUG
if (message.name === "SAVE_LOCATION_CHANGED") {
window.location.reload();
}
});
</script>
<svelte:window on:keydown={preventKeyCommands} on:resize={windowResize}/>
{#if $state.includes("init")}
<div class="initializer" out:blur="{{duration: 350}}">
{@html logo}
{#if $state.includes("init-error")}
<span>Failed to initialize app, check the browser console</span>
{:else}
<span>Initializing app...</span>
{/if}
</div>
{/if}
<div>
<Sidebar/>
<Editor/>
</div>
<ul>
{#each $notifications as item (item.id)}
<Notification on:click={() => notifications.remove(item.id)} {item}/>
{/each}
</ul>
{#if $state.includes("settings")}<Settings/>{/if}
<style>
.initializer {
align-items: center;
background-color: var(--color-bg-primary);
display: flex;
flex-direction: column;
height: 100%;
justify-content: center;
left: 0;
opacity: 1;
position: absolute;
top: 0;
visibility: visible;
width: 100%;
z-index: 99;
}
.initializer :global(svg) {
height: 1.5rem;
margin-bottom: 0.5rem;
}
.initializer span {
color: var(--text-color-secondary);
font: var(--text-medium);
}
div:not(.initializer) {
display: flex;
flex: 1 0 0;
overflow: hidden;
}
ul {
bottom: 1rem;
left: 50%;
position: fixed;
transform: translateX(-50%);
z-index: 98;
}
</style>