Skip to content

Commit c29e3eb

Browse files
committed
Accept auto-fill from URL & Auto-copy to clipboard & notify parent window on migration
1 parent 6e6a0f4 commit c29e3eb

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

app.vue

+28-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
const tex = ref('');
33
const message = ref<null | { content: string, type: 'success' | 'error' }>(null);
44
5-
function onMigrate() {
5+
function migrate() {
6+
// 1. Migrate the TeX
67
try {
78
const migrated = safeMigrateTex(tex.value);
89
if (migrated === tex.value) {
@@ -13,16 +14,40 @@ function onMigrate() {
1314
message.value = { content: 'Migrated successfully', type: 'success' };
1415
} catch (error: any) {
1516
message.value = { content: error?.message ?? 'Unknown error', type: 'error' };
17+
return;
18+
}
19+
20+
// 2. Copy the migrated TeX to the clipboard
21+
try {
22+
navigator.clipboard.writeText(tex.value);
23+
} catch (error: any) {
24+
message.value = { content: error?.message ?? 'Unknown error when copying to clipboard', type: 'error' };
25+
return;
26+
}
27+
28+
// 3. Notify the parent window
29+
if (window.parent !== window) {
30+
window.parent.postMessage({ type: 'migrated', tex: tex.value }, '*');
1631
}
1732
}
33+
34+
// On mount, get the hash from the URL and set it to the textarea
35+
onMounted(() => {
36+
const hash = window.location.hash.slice(1);
37+
if (hash) {
38+
tex.value = decodeURIComponent(hash);
39+
migrate();
40+
}
41+
});
42+
1843
</script>
1944

2045
<template>
2146
<div class="container">
2247
<textarea v-model="tex"></textarea>
2348
<p v-if="message" :class="'message message-' + message.type">{{ message.content }}</p>
2449
<div class="actions">
25-
<button @click="onMigrate">Migrate TeX</button>
50+
<button @click="migrate">Migrate TeX</button>
2651
</div>
2752
</div>
2853
</template>
@@ -89,4 +114,4 @@ button {
89114
background-color: #f5f5f5;
90115
cursor: pointer;
91116
}
92-
</style>
117+
</style>

0 commit comments

Comments
 (0)