-
Notifications
You must be signed in to change notification settings - Fork 57
feat(tutorial): refine editor-leave confirmation and course success modal navigation #3305
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
base: dev
Are you sure you want to change the base?
Changes from all commits
cc2da42
c3b510a
d22a085
8fda240
c2a7a2e
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { shallowRef } from 'vue' | ||
|
|
||
| import type { LocaleMessage } from '@/utils/i18n' | ||
|
|
||
| // Max age (ms) for a skip request to still suppress the leave confirmation. Time-bound so a | ||
| // request that is never consumed cannot get "stuck": some navigations (e.g. editor -> editor | ||
| // route updates, or starting a course from a non-editor page) never reach the editor's | ||
| // `onBeforeRouteLeave`, and an un-expiring request would otherwise suppress a later, genuine | ||
| // leave confirmation. | ||
| const skipConfirmTimeout = 1000 | ||
|
|
||
| /** | ||
| * Controls the editor's "leave editor" confirmation. | ||
| * | ||
| * This is an editor-owned extension point: other features (e.g. tutorials) can drive the | ||
| * editor's leave behavior without the editor depending on them. The editor only exposes | ||
| * generic capabilities here and stays ignorant of who is driving it. | ||
| * | ||
| * - `messageOverride`: replace the confirmation message (e.g. tutorial-specific copy). | ||
| * - `requestSkipOnce` / `consumeSkipOnce`: skip the confirmation for a single, expected | ||
| * navigation (e.g. starting a course, or returning to the course series). | ||
| */ | ||
| class EditorLeaveConfirm { | ||
| private messageOverrideRef = shallowRef<LocaleMessage | null>(null) | ||
|
|
||
| get messageOverride(): LocaleMessage | null { | ||
| return this.messageOverrideRef.value | ||
| } | ||
| setMessageOverride(message: LocaleMessage | null) { | ||
| this.messageOverrideRef.value = message | ||
| } | ||
|
|
||
| private skipRequestedAt = 0 | ||
| requestSkipOnce() { | ||
| this.skipRequestedAt = Date.now() | ||
| } | ||
| consumeSkipOnce() { | ||
| const skip = Date.now() - this.skipRequestedAt < skipConfirmTimeout | ||
| this.skipRequestedAt = 0 | ||
| return skip | ||
| } | ||
| } | ||
|
|
||
| // App-level single instance: there is only ever one editor leave confirmation in play, and | ||
| // the drivers (tutorial, success modal) live outside the editor component tree, so they reach | ||
| // it by import rather than provide/inject. | ||
| export const editorLeaveConfirm = new EditorLeaveConfirm() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { useIsRouteLoaded } from '@/utils/route-loading' | |
| import { isTutorialTopic, provideTutorial, Tutorial } from './tutorial' | ||
|
|
||
| import { useCopilot } from '@/components/copilot/context' | ||
| import { editorLeaveConfirm } from '@/components/editor/leave-confirm' | ||
| import * as tutorialCourseSuccess from './TutorialCourseSuccess.vue' | ||
| import * as tutorialCourseExitLink from './TutorialCourseExitLink' | ||
| import * as tutorialStateIndicator from './TutorialStateIndicator.vue' | ||
|
|
@@ -27,7 +28,15 @@ watch( | |
| (currentCourse, _, onCleanup) => { | ||
| if (currentCourse == null) return | ||
|
|
||
| // Drive the editor's leave confirmation with tutorial-specific copy while a course is | ||
| // active; the editor exposes the override and stays unaware of tutorials. | ||
| editorLeaveConfirm.setMessageOverride({ | ||
| en: 'Tutorial edits will not be saved if you leave now. Are you sure to leave?', | ||
|
Collaborator
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. 这个文案跟默认文案表达的信息差别并不大,有没有可能优化默认文案来让它在不同场景下都比较合适,而不是针对 tutorial 使用不一样的文案? |
||
| zh: '教程中的修改不会被保存,确认要离开吗?' | ||
| }) | ||
|
|
||
| const disposers = [ | ||
| () => editorLeaveConfirm.setMessageOverride(null), | ||
| copilot.registerCustomElement({ | ||
| tagName: tutorialCourseSuccess.tagName, | ||
| description: tutorialCourseSuccess.detailedDescription, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { timeout, until } from '@/utils/utils' | |
| import { userSessionStorageRef } from '@/utils/user-storage' | ||
| import type { Copilot, Topic } from '@/components/copilot/copilot' | ||
| import { tagName as highlightLinkTagName } from '@/components/copilot/markdown-elements/HighlightLink.vue' | ||
| import { editorLeaveConfirm } from '@/components/editor/leave-confirm' | ||
| import type { Course } from '@/apis/course' | ||
| import type { CourseSeries } from '@/apis/course-series' | ||
|
|
||
|
|
@@ -71,6 +72,10 @@ export class Tutorial { | |
| const { entrypoint } = course | ||
|
|
||
| if (entrypoint) { | ||
| // A course entrypoint may point to a non-editor route. Navigating away from the | ||
| // editor would otherwise trigger its leave confirmation, but starting a course is | ||
| // an explicit, expected action, so skip the confirmation for this navigation. | ||
| editorLeaveConfirm.requestSkipOnce() | ||
|
Collaborator
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. 跳过 confirm 的逻辑放到 TutorialCourseSuccessModal.vue 中调用 startCourse 的地方做可能更合适?按我们讨论的结论,不是所有的 |
||
| await this.router.push(entrypoint) | ||
| await until(this.isRouteLoaded) | ||
| await timeout(100) // Wait for detailed UI rendering | ||
|
|
||
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.
通过单独的机制来避免跟 editor 跟 tutorial 逻辑的耦合,这个思路我觉得没问题。不过现在这个 editor-leave-confirm 稍微有点重,我在想能不能借用已有的 editing-dirty 来达到这次的目的;比如 class Editing 增加个方法来重置 dirty 状态,现在 requestSkipOnce 的地方通过重置 dirty 状态来跳过检查?