From b22f32f6f1bd4c0beecad423134ad57f7b6a6553 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Fri, 2 Dec 2022 21:09:52 -0800 Subject: [PATCH] generating stable feature id --- web/app/cad/craft/operationBundle.ts | 12 +++++--- .../cad/craft/schema/initializeBySchema.ts | 4 ++- web/app/cad/craft/ui/HistoryTimeline.jsx | 2 +- .../cad/craft/ui/SelectedModificationInfo.jsx | 3 +- web/app/cad/projectBundle.ts | 28 +++++++++++++++++-- .../projectManager/projectManagerBundle.ts | 8 +++++- 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/web/app/cad/craft/operationBundle.ts b/web/app/cad/craft/operationBundle.ts index 1e8d1899..cce0d4ca 100644 --- a/web/app/cad/craft/operationBundle.ts +++ b/web/app/cad/craft/operationBundle.ts @@ -99,7 +99,7 @@ export function activate(ctx: ApplicationContext) { ctx.actionService.registerActions(actions); } - function get(id: string): Operation { + function get(id: string): Operation { const op = registry$.value[id]; if (!op) { throw `operation ${id} is not registered`; @@ -115,7 +115,11 @@ export function activate(ctx: ApplicationContext) { ctx.services.operation = ctx.operationService; } -export interface Operation extends OperationDescriptor{ +interface FeatureIdParam { + featureId: string; +} + +export interface Operation extends OperationDescriptor{ appearance: { id: string; label: string; @@ -133,7 +137,7 @@ type OpIcon = IconDeclaration | IconType | string | ((props: any) => JSX.Element type MaterializedParams = any; -export interface OperationDescriptor { +export interface OperationDescriptor { id: string; label: string; info: string; @@ -162,7 +166,7 @@ export interface OperationDescriptor { export interface OperationService { registerOperations(descriptior: OperationDescriptor[]); - get(operationId: string): Operation; + get(operationId: string): Operation; } export type Index = { diff --git a/web/app/cad/craft/schema/initializeBySchema.ts b/web/app/cad/craft/schema/initializeBySchema.ts index 5b20b166..1e32ba4e 100644 --- a/web/app/cad/craft/schema/initializeBySchema.ts +++ b/web/app/cad/craft/schema/initializeBySchema.ts @@ -4,7 +4,9 @@ import {ApplicationContext} from "cad/context"; export default function initializeBySchema(schema: OperationSchema, context: ApplicationContext) { const fields = Object.keys(schema); - const obj = {}; + const obj = { + featureId: context.projectService.counterGenerator.generateFeatureId() + }; for (const field of fields) { let val = undefined; const md = schema[field] as SchemaField; diff --git a/web/app/cad/craft/ui/HistoryTimeline.jsx b/web/app/cad/craft/ui/HistoryTimeline.jsx index 118169aa..7e96ff08 100644 --- a/web/app/cad/craft/ui/HistoryTimeline.jsx +++ b/web/app/cad/craft/ui/HistoryTimeline.jsx @@ -139,7 +139,7 @@ function HistoryItem({index, pointer, modification, getOperation, toggle, select return
toggle(index, modification, e.currentTarget)}> - { index + 1 } + { modification.params.featureId }
; }); diff --git a/web/app/cad/craft/ui/SelectedModificationInfo.jsx b/web/app/cad/craft/ui/SelectedModificationInfo.jsx index 110e3335..1bdbca1b 100644 --- a/web/app/cad/craft/ui/SelectedModificationInfo.jsx +++ b/web/app/cad/craft/ui/SelectedModificationInfo.jsx @@ -43,12 +43,11 @@ function SelectedModificationInfo({ history, index, } const appearance = resolveAppearance(op, m.params); - const indexNumber = index + 1; return
diff --git a/web/app/cad/projectBundle.ts b/web/app/cad/projectBundle.ts index 2e094bea..c7a4f5c3 100644 --- a/web/app/cad/projectBundle.ts +++ b/web/app/cad/projectBundle.ts @@ -2,7 +2,7 @@ import {setSketchPrecision} from './sketch/sketchReader'; import {runSandbox} from './sandbox'; import {LOG_FLAGS} from './logFlags'; import {ApplicationContext} from "cad/context"; -import {ProjectModel} from "./projectManager/projectManagerBundle"; +import {ProjectCounters, ProjectModel} from "./projectManager/projectManagerBundle"; import {DebugMode$} from "debugger/Debugger"; import {fillUpMissingFields} from "cad/craft/schema/initializeBySchema"; @@ -51,6 +51,7 @@ export function initProjectService(ctx: ApplicationContext, id: string, hints: a if (!currentWorkbench?.internal && ctx.workbenchService.defaultWorkbenchId !== currentWorkbench.workbenchId) { data.workbench = currentWorkbench.workbenchId; } + data.counters = counterGenerator.projectCounters; ctx.storageService.set(projectStorageKey(), JSON.stringify(data)); } @@ -74,9 +75,15 @@ export function initProjectService(ctx: ApplicationContext, id: string, hints: a const operation = ctx.operationService.get(req.type); if (operation) { fillUpMissingFields(req.params, operation.schema, ctx); + if (!req.params.featureId) { + req.params.featureId = counterGenerator.generateFeatureId(); + } } }); } + if (!data.counters) { + data.counters = new ProjectCounters(); + } } function loadWorkbench(data: ProjectModel) { @@ -99,6 +106,7 @@ export function initProjectService(ctx: ApplicationContext, id: string, hints: a ctx.assemblyService.loadConstraints(data.assembly); } + counterGenerator = new ProjectCounterGenerator(data.counters); } function empty() { @@ -108,9 +116,11 @@ export function initProjectService(ctx: ApplicationContext, id: string, hints: a }); } + let counterGenerator = new ProjectCounterGenerator(new ProjectCounters()); + ctx.projectService = { id, sketchStorageKey, projectStorageKey, sketchStorageNamespace, getSketchURL, save, load, loadData, empty, - hints + hints, counterGenerator }; } @@ -161,6 +171,19 @@ function processParams(params, context) { } } +export class ProjectCounterGenerator { + + projectCounters: ProjectCounters; + + constructor(projectCounters) { + this.projectCounters = projectCounters; + } + + generateFeatureId(): string { + return (++this.projectCounters.featureId) + ""; + } +} + export interface ProjectService { readonly id: string; @@ -183,6 +206,7 @@ export interface ProjectService { empty(): void; + counterGenerator: ProjectCounterGenerator; } export interface ProjectBundleContext { diff --git a/web/app/cad/projectManager/projectManagerBundle.ts b/web/app/cad/projectManager/projectManagerBundle.ts index 377f2000..2f75409b 100644 --- a/web/app/cad/projectManager/projectManagerBundle.ts +++ b/web/app/cad/projectManager/projectManagerBundle.ts @@ -197,6 +197,10 @@ export function activate(ctx: ApplicationContext) { ctx.services.projectManager = ctx.projectManager; } +export class ProjectCounters { + featureId: number = 0; +} + export interface ProjectModel { history: OperationRequest[], @@ -205,7 +209,9 @@ export interface ProjectModel { assembly?: AssemblyConstraintDefinition[]; - workbench?: string + workbench?: string, + + counters?: ProjectCounters; } export interface ModelBundle {