11import type { Monaco } from "@monaco-editor/react" ;
22import confetti from "canvas-confetti" ;
3- import type { editor } from "monaco-editor" ;
3+ import { editor } from "monaco-editor" ;
44import { toast } from "react-toastify" ;
55import { create } from "zustand" ;
66import { kaplayVersions } from "../data/kaplayVersions.json" ;
@@ -27,6 +27,14 @@ interface EditorRuntime {
2727 * The current selection in the editor
2828 */
2929 currentFile : string ;
30+ /**
31+ * The last saved editor value
32+ */
33+ editorLastSavedValue : string | null ;
34+ /**
35+ * If file was modified and not saved
36+ */
37+ hasUnsavedChanges : boolean ;
3038 /**
3139 * Decorations for the gylph images
3240 */
@@ -68,6 +76,8 @@ export interface EditorStore {
6876 updateModelMarkers : ( ) => void ;
6977 showNotification : ( message : string ) => void ;
7078 setEditorValue : ( value : string ) => void ;
79+ updateEditorLastSavedValue : ( value ?: string ) => void ;
80+ updateHasUnsavedChanges : ( ) => void ;
7181 updateAndRun : ( ) => void ;
7282}
7383
@@ -76,6 +86,8 @@ export const useEditor = create<EditorStore>((set, get) => ({
7686 editor : null ,
7787 monaco : null ,
7888 currentFile : "main.js" ,
89+ editorLastSavedValue : null ,
90+ hasUnsavedChanges : false ,
7991 gylphDecorations : null ,
8092 iframe : null ,
8193 console : null ,
@@ -143,6 +155,8 @@ export const useEditor = create<EditorStore>((set, get) => ({
143155 viewStates : viewStates ,
144156 } ,
145157 } ) ) ;
158+
159+ get ( ) . updateEditorLastSavedValue ( ) ;
146160 } ,
147161 update : ( customValue ?: string ) => {
148162 if ( customValue ) {
@@ -163,6 +177,8 @@ export const useEditor = create<EditorStore>((set, get) => ({
163177 currentFile . path . slice ( 0 , 25 ) + "..." ,
164178 ) ;
165179
180+ get ( ) . updateEditorLastSavedValue ( currentFile . value ) ;
181+
166182 get ( ) . setEditorValue ( currentFile . value ) ;
167183 get ( ) . updateImageDecorations ( ) ;
168184 } ,
@@ -315,6 +331,27 @@ export const useEditor = create<EditorStore>((set, get) => ({
315331
316332 editor . setValue ( value ) ;
317333 } ,
334+ updateEditorLastSavedValue ( value ) {
335+ set ( ( state ) => ( {
336+ runtime : {
337+ ...state . runtime ,
338+ editorLastSavedValue :
339+ ( value ?? get ( ) . runtime . editor ?. getValue ( ) ) ?? null ,
340+ } ,
341+ } ) ) ;
342+ } ,
343+ updateHasUnsavedChanges ( ) {
344+ const editor = get ( ) . runtime . editor ;
345+ if ( ! editor ) return ;
346+
347+ set ( ( state ) => ( {
348+ runtime : {
349+ ...state . runtime ,
350+ hasUnsavedChanges : get ( ) . getRuntime ( ) . editorLastSavedValue
351+ != editor . getValue ( ) ,
352+ } ,
353+ } ) ) ;
354+ } ,
318355 updateAndRun ( ) {
319356 get ( ) . getRuntime ( ) . editor ?. setScrollTop ( 0 ) ;
320357 get ( ) . update ( ) ;
0 commit comments