Skip to content

Commit

Permalink
Add ability to rename a project
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas authored and av8ta committed May 26, 2024
1 parent c9a2cbe commit 0e9c864
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Watch vitest unit tests only:

```shell
cd applications/web
pnpm test:unit -w
pnpm test:unit -w
```

### rust
Expand Down Expand Up @@ -135,7 +135,6 @@ Github project for tracking progress is [here](https://github.com/orgs/CADmium-C
- [ ] Extrusion
- [ ] Configure an extrusion to create new solid or subtract from existing solid
- [ ] Project
- [ ] Ability to rename the project
- [ ] Ability to delete steps
- [ ] bind ctrl + s to .cadmium export, and ctrl + o to .cadmium import
- [ ] Units
Expand Down
33 changes: 32 additions & 1 deletion applications/web/src/routes/(CADmium)/AppBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import type { WithTarget } from "shared/types"
import { isProject } from "shared/typeGuards"
import { base } from "$app/paths"
import { renameProject } from "shared/projectUtils"
// prettier-ignore
const log = (function () { const context = "[AppBar.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})()
export let userName = "mattferraro.dev"
export let project: Project
export let renaming: boolean = false
export let newProjectName: string = ""
export let newFileContent: string | null = null
Expand Down Expand Up @@ -46,7 +49,35 @@
<img class="object-cover h-10 w-10 ml-4" alt="logo" src="{base}/cadmium_logo_min.svg" />
</div>
<div class="select-none">CADmium</div>
<div class="text-xl font-medium">{project.name ?? ""}</div>
{#if renaming}
<input
class="bg-gray-300 text-gray-700 py-2 px-4 text-xl font-medium"
type="text"
bind:value={newProjectName}
on:blur={() => {
log("Renaming project aborted")
renaming = false
newProjectName = project.name ?? ""
}}
on:keydown={(e) => {
if (e.key === "Enter") {
log("Renaming project")
renameProject(newProjectName)
project.name = newProjectName
renaming = false
}
}}
/>
{:else}
<div
class="text-xl font-medium"
on:dblclick={() => {
log("Renaming project")
renaming = true
newProjectName = project.name ?? ""
}}
>{project.name ?? ""}</div>
{/if}
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="hover:bg-gray-300 rounded p-1"
Expand Down
19 changes: 19 additions & 0 deletions packages/shared/projectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
isNewPointOnSketch2,
isNewRectangleBetweenPoints,
isNewSketchOnPlane,
isRenameProject,
isRenameStep,
isRenameWorkbench,
isSetSketchPlane,
Expand Down Expand Up @@ -367,6 +368,17 @@ export function renameWorkbench(newName: string): void {
sendWasmMessage(message)
}

export function renameProject(newName: string): void {
log("[renameProject] newName", newName)
const message: Message = {
RenameProject: {
new_name: newName
}
}
checkWasmMessage(message)
sendWasmMessage(message)
}

// If the project ever becomes stale, refresh it. This should be pretty rare.
projectIsStale.subscribe((value) => {
if (value) {
Expand Down Expand Up @@ -624,6 +636,13 @@ function checkWasmMessage(message: Message, abort = true, logError = true): bool
}
return true

case "RenameProject":
if (!isRenameProject(command)) {
logOrAbort()
return false
}
return true

default:
console.error("[projectUtils.ts] [checkWasmMessage]", "messageType typeGuard not implemented:", key)
return false
Expand Down
15 changes: 13 additions & 2 deletions packages/shared/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ import type {
RenameStep,
Message,
MessageHistory,
RenameWorkbench
RenameWorkbench,
RenameProject
} from "./types"
import { Vector2 } from "three"
import { Vector3 } from "three"
Expand Down Expand Up @@ -1118,6 +1119,14 @@ export function isRenameWorkbench(obj: unknown): obj is RenameWorkbench {
)
}

export function isRenameProject(obj: unknown): obj is RenameProject {
const typedObj = obj as RenameProject
return (
((typedObj !== null && typeof typedObj === "object") || typeof typedObj === "function") &&
typeof typedObj["new_name"] === "string"
)
}

export function isMessage(obj: unknown): obj is Message {
const typedObj = obj as Message
return (
Expand Down Expand Up @@ -1146,7 +1155,9 @@ export function isMessage(obj: unknown): obj is Message {
(((typedObj !== null && typeof typedObj === "object") || typeof typedObj === "function") &&
(isRenameStep(typedObj["RenameStep"]) as boolean)) ||
(((typedObj !== null && typeof typedObj === "object") || typeof typedObj === "function") &&
(isRenameWorkbench(typedObj["RenameWorkbench"]) as boolean))
(isRenameWorkbench(typedObj["RenameWorkbench"]) as boolean)) ||
(((typedObj !== null && typeof typedObj === "object") || typeof typedObj === "function") &&
(isRenameProject(typedObj["RenameProject"]) as boolean))
)
}

Expand Down
5 changes: 5 additions & 0 deletions packages/shared/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ interface RenameWorkbench {
new_name: string
}

interface RenameProject {
new_name: string
}

export type Message_GeneratedFromRust =
| { RenameWorkbench: { workbench_id: number; new_name: string } }
| { RenameStep: { workbench_id: number; step_id: number; new_name: string } }
Expand Down Expand Up @@ -348,6 +352,7 @@ type Message =
| { NewPointOnSketch2: NewPointOnSketch2 }
| { RenameStep: RenameStep }
| { RenameWorkbench: RenameWorkbench }
| { RenameProject: RenameProject }

interface MessageHistory {
message: Message
Expand Down

0 comments on commit 0e9c864

Please sign in to comment.