File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed
Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 1212 </div >
1313
1414 <textarea
15+ ref =" textarea"
1516 v-model =" previewStore.files[props.filename]"
1617 class =" textarea w-full min-h-full font-mono focus:outline-0 text-slate-700 dark:text-slate-300"
1718 placeholder =" Every journey begins with a first step"
@@ -31,4 +32,23 @@ function loadSample() {
3132 previewStore .files [props .filename ] =
3233 previewStore .syntax == Syntax .RST ? sampleRst : sampleMd ;
3334}
35+
36+ // *** Warn dialog before reload ******************************************************
37+
38+ onMounted (() => {
39+ window .addEventListener (" beforeunload" , handleBeforeUnload );
40+ });
41+
42+ onBeforeUnmount (() => {
43+ window .removeEventListener (" beforeunload" , handleBeforeUnload );
44+ });
45+
46+ const textarea = useTemplateRef (" textarea" );
47+
48+ function handleBeforeUnload(e : BeforeUnloadEvent ) {
49+ if (textarea && textarea .value ! .value .trim () !== " " ) {
50+ // this will show generic browser dialog, it cannot be customized
51+ e .preventDefault ();
52+ }
53+ }
3454 </script >
Original file line number Diff line number Diff line change 66
77 <div class =" tooltip tooltip-left" data-tip =" Switch syntax" >
88 <select
9- v-model =" previewStore.syntax"
9+ :value =" previewStore.syntax"
1010 class =" select select-ghost text-base"
11+ @change =" onSyntaxChange"
1112 >
1213 <option :value =" Syntax.RST" selected >reStructuredText</option >
1314 <option :value =" Syntax.MD" >Markdown</option >
5455
5556<script setup lang="ts">
5657const previewStore = usePreviewStore ();
58+
59+ // *** Warn/prevent before syntax switching ********************************************
60+
61+ let previousSyntax = previewStore .syntax ;
62+
63+ function onSyntaxChange(event : Event ) {
64+ const selectElement = event .target as HTMLSelectElement ;
65+ const confirmed = window .confirm (
66+ " Switching a syntax will clear your content. Do you want to continue?"
67+ );
68+ if (confirmed ) {
69+ // Switch syntax
70+ const newSyntax = Syntax [selectElement .value as keyof typeof Syntax ];
71+ previewStore .syntax = newSyntax ;
72+ previousSyntax = newSyntax ;
73+ } else {
74+ // Prevent the change by resetting the value
75+ selectElement .value = previousSyntax ;
76+ }
77+ }
5778 </script >
You can’t perform that action at this time.
0 commit comments