Skip to content
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

Event Emission from viewmodel to Event Manager and Rename Project Flow Update #645

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const OneTimeAction = {
Idle: { type: 'Idle' } as OneTimeActionType,

ProjectAction: {
RenameCurrentProject: (newName: string) => ({ type: 'RenameCurrentProject', newName }),
RenameCurrentProject: (newName: string): OneTimeActionType => ({ type: 'RenameCurrentProject', newName }),
NewProject: { type: 'NewProject' } as OneTimeActionType,
ExportSelectedShapes: { type: 'ExportSelectedShapes' } as OneTimeActionType,
SwitchProject: (projectId: string): OneTimeActionType => ({ type: 'SwitchProject', projectId }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dismiss the modal correctly.
<script lang="ts">
import { onMount } from 'svelte';

export let onSubmit: () => void = () => {};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve initialized it as an empty function to ensure it's optional—other components using this one won’t need any changes, and everything will work as before if the function isn’t needed.

export let onDismiss: () => void;
export let left: number = 0;
export let top: number = 0;
Expand All @@ -27,6 +28,8 @@ onMount(() => {
function onKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
dismiss();
} else if (event.key === 'Enter') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should handle this key inside the modal component container. This key should be captured explicitly by the content of the modal.

submit();
}
}

Expand Down Expand Up @@ -62,6 +65,12 @@ function dismiss() {
onDismiss();
}

function submit() {
checkbox = null;
onSubmit();
onDismiss();
}

function createStyle() {
let adjustedLeft = left + width > window.innerWidth ? window.innerWidth - width : left;
return [
Expand Down
24 changes: 21 additions & 3 deletions monosketch-svelte/src/lib/ui/modal/recent-project/viewmodel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Flow } from '../../../libs/flow';
import { type ProjectItem, sampleProjectItems } from './model';
import { UUID } from '../../../mono/uuid';
import type { ActionManager } from '$mono/action-manager/action-manager';
import { OneTimeAction } from '$mono/action-manager/one-time-actions';
import { AppContext } from '$app/app-context';

class ProjectDataViewModel {
private _projectFlow: Flow<ProjectItem[]> = new Flow();
Expand All @@ -10,6 +13,8 @@ class ProjectDataViewModel {
deletingProjectIdFlow: Flow<string> = new Flow('');
renamingProjectIdFlow: Flow<string> = new Flow('');

constructor(private actionManager: ActionManager) {}

setProjectList(projectList: ProjectItem[]) {
this._projectFlow.value = projectList;
this.openingProjectIdFlow.value = projectList[0].id;
Expand All @@ -22,11 +27,18 @@ class ProjectDataViewModel {
};
this.setProjectList([newProject, ...this._projectFlow.value!]);
this.openProject(newProject.id);
this.setRenamingProject(newProject.id);
this.setRenamingProject(newProject.id, newProject.name);
this.actionManager.setOneTimeAction(OneTimeAction.ProjectAction.NewProject);
}

openProject(id: string) {
this.openingProjectIdFlow.value = id;
// Reorder the project list, move the opened project to the top
const currentProject = this._projectFlow.value!.find((item) => item.id === id);
if (currentProject) {
this._projectFlow.value = [currentProject, ...this._projectFlow.value!.filter((item) => item.id !== id)];
}
this.actionManager.setOneTimeAction(OneTimeAction.ProjectAction.SwitchProject(id));
}

confirmDeletingProject(id: string) {
Expand All @@ -35,14 +47,17 @@ class ProjectDataViewModel {

deleteProject(id: string) {
this._projectFlow.value = this._projectFlow.value!.filter((item) => item.id !== id);
this.actionManager.setOneTimeAction(OneTimeAction.ProjectAction.RemoveProject(id));
}

cancelDeletingProject() {
this.deletingProjectIdFlow.value = '';
}

setRenamingProject(id: string) {
setRenamingProject(id: string, newName: string) {
console.log("Project id: " + id);
this.renamingProjectIdFlow.value = id;
this.actionManager.setOneTimeAction(OneTimeAction.ProjectAction.RenameCurrentProject(newName));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. setRenamingProject means set the target project for renaming, not setting the project name. Set project name is done by setProjectName

}

setProjectName(id: string, name: string) {
Expand All @@ -55,12 +70,15 @@ class ProjectDataViewModel {
}
return item;
});
this.actionManager.setOneTimeAction(OneTimeAction.ProjectAction.RenameCurrentProject(name));
}

getProject(id: string): ProjectItem | undefined {
console.log(this._projectFlow.value);
return this._projectFlow.value!.find((item) => item.id === id);
}
}

export const projectDataViewModel = new ProjectDataViewModel();
const appContext = new AppContext();
export const projectDataViewModel = new ProjectDataViewModel(appContext.actionManager);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. appContext should be achieved from the context of Svelte.
Then we will set projectDataViewModel into the context for internal component used.

projectDataViewModel.setProjectList(sampleProjectItems);
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ $: left = model.targetBounds.left;
$: top = model.targetBounds.top + model.targetBounds.height + 6;

function onDismiss() {
modalViewModel.renamingProjectModalStateFlow.value = null;
}

function onSubmit() {
if (name) {
projectDataViewModel.setProjectName(model.id, name);
}
projectDataViewModel.setRenamingProject('');
projectDataViewModel.setRenamingProject(model.id, name);
modalViewModel.renamingProjectModalStateFlow.value = null;
}

Expand All @@ -29,6 +33,6 @@ onMount(() => {
});
</script>

<NoBackgroundModal {onDismiss} {left} {top} width="{180}">
<NoBackgroundModal {onDismiss} {onSubmit} {left} {top} width="{180}">
<ProjectNameTextField bind:name />
</NoBackgroundModal>
Loading