-
Notifications
You must be signed in to change notification settings - Fork 41
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
feature: Warn user if they leave the /editor page with unsaved changes #109 #139
Changes from all commits
c4e5b60
3dfb23d
c54b41d
ce6682d
870ed44
3625d3f
eb90f9d
44d06ce
979698c
9b1b530
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ export const CodeEditor = ({ | |
onDtsChange?: (dts: string) => void | ||
initialCode: string | ||
readOnly?: boolean | ||
manualEditsJson?: string | ||
isStreaming?: boolean | ||
manualEditsFileContent: string | ||
showImportAndFormatButtons?: boolean | ||
|
@@ -86,7 +87,6 @@ export const CodeEditor = ({ | |
Object.entries(files).forEach(([filename, content]) => { | ||
fsMap.set(filename, content) | ||
}) | ||
|
||
createDefaultMapFromCDN( | ||
{ target: ts.ScriptTarget.ES2022 }, | ||
"5.6.3", | ||
|
@@ -128,9 +128,13 @@ export const CodeEditor = ({ | |
.replace(registryPrefixes[2], "") | ||
const packageName = fullPackageName.split("/")[0].replace(/\./, "/") | ||
const pathInPackage = fullPackageName.split("/").slice(1).join("/") | ||
const jsdelivrPath = `${packageName}${pathInPackage ? `/${pathInPackage}` : ""}` | ||
const jsdelivrPath = `${packageName}${ | ||
pathInPackage ? `/${pathInPackage}` : "" | ||
}` | ||
return fetch( | ||
`${apiUrl}/snippets/download?jsdelivr_resolve=${input.includes("/resolve/")}&jsdelivr_path=${encodeURIComponent(jsdelivrPath)}`, | ||
`${apiUrl}/snippets/download?jsdelivr_resolve=${input.includes( | ||
"/resolve/", | ||
)}&jsdelivr_path=${encodeURIComponent(jsdelivrPath)}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert unnecessary change here |
||
) | ||
} | ||
return fetch(input, init) | ||
|
@@ -263,7 +267,7 @@ export const CodeEditor = ({ | |
for (let pos = from; pos < to; ) { | ||
const line = view.state.doc.lineAt(pos) | ||
const lineText = line.text | ||
const matches = lineText.matchAll(/@tsci\/[\w\-.]+/g) | ||
const matches = lineText.matchAll(/@tsci\/[\w.]+/g) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would introduce a bug, revert There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sorry for all this trouble i am thinking to shut this pr and create a cleaner pr that is easier to manage for you and ofcourse myself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes that's always ok! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and no problem- my messages are short because i often type them from my phone, not because i'm angry at you 😁 |
||
for (const match of matches) { | ||
if (match.index !== undefined) { | ||
const start = line.from + match.index | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect } from "react"; | ||
export default function useWarnUserForUnsavedChanges({ | ||
hasUnsavedChanges, | ||
}: { | ||
hasUnsavedChanges: boolean; | ||
}) { | ||
useEffect(() => { | ||
const handleBeforeUnload = (event: BeforeUnloadEvent) => { | ||
if (hasUnsavedChanges) { | ||
event.preventDefault(); | ||
event.returnValue = ""; // Shows the confirmation dialog on reload or close if there are unsaved changes | ||
} | ||
}; | ||
|
||
// Attach event listeners | ||
window.addEventListener("beforeunload", handleBeforeUnload); | ||
|
||
// Cleanup event listeners on component unmount | ||
return () => { | ||
window.removeEventListener("beforeunload", handleBeforeUnload); | ||
}; | ||
}, [hasUnsavedChanges]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert unnecessary change here