generating stable feature id

This commit is contained in:
Val Erastov 2022-12-02 21:09:52 -08:00
parent 694bbb8c09
commit b22f32f6f1
6 changed files with 46 additions and 11 deletions

View file

@ -99,7 +99,7 @@ export function activate(ctx: ApplicationContext) {
ctx.actionService.registerActions(actions); ctx.actionService.registerActions(actions);
} }
function get<T>(id: string): Operation<T> { function get<T extends FeatureIdParam>(id: string): Operation<T> {
const op = registry$.value[id]; const op = registry$.value[id];
if (!op) { if (!op) {
throw `operation ${id} is not registered`; throw `operation ${id} is not registered`;
@ -115,7 +115,11 @@ export function activate(ctx: ApplicationContext) {
ctx.services.operation = ctx.operationService; ctx.services.operation = ctx.operationService;
} }
export interface Operation<R> extends OperationDescriptor<R>{ interface FeatureIdParam {
featureId: string;
}
export interface Operation<R extends FeatureIdParam> extends OperationDescriptor<R>{
appearance: { appearance: {
id: string; id: string;
label: string; label: string;
@ -133,7 +137,7 @@ type OpIcon = IconDeclaration | IconType | string | ((props: any) => JSX.Element
type MaterializedParams = any; type MaterializedParams = any;
export interface OperationDescriptor<R> { export interface OperationDescriptor<R extends FeatureIdParam> {
id: string; id: string;
label: string; label: string;
info: string; info: string;
@ -162,7 +166,7 @@ export interface OperationDescriptor<R> {
export interface OperationService { export interface OperationService {
registerOperations(descriptior: OperationDescriptor<any>[]); registerOperations(descriptior: OperationDescriptor<any>[]);
get<T>(operationId: string): Operation<T>; get<T extends FeatureIdParam>(operationId: string): Operation<T>;
} }
export type Index<T> = { export type Index<T> = {

View file

@ -4,7 +4,9 @@ import {ApplicationContext} from "cad/context";
export default function initializeBySchema(schema: OperationSchema, context: ApplicationContext) { export default function initializeBySchema(schema: OperationSchema, context: ApplicationContext) {
const fields = Object.keys(schema); const fields = Object.keys(schema);
const obj = {}; const obj = {
featureId: context.projectService.counterGenerator.generateFeatureId()
};
for (const field of fields) { for (const field of fields) {
let val = undefined; let val = undefined;
const md = schema[field] as SchemaField; const md = schema[field] as SchemaField;

View file

@ -139,7 +139,7 @@ function HistoryItem({index, pointer, modification, getOperation, toggle, select
return <div className={cx(ls.historyItem, selected&&ls.selected, disabled&&ls.disabled, inProgress&&ls.inProgress)} return <div className={cx(ls.historyItem, selected&&ls.selected, disabled&&ls.disabled, inProgress&&ls.inProgress)}
onClick={e => toggle(index, modification, e.currentTarget)}> onClick={e => toggle(index, modification, e.currentTarget)}>
<ImgIcon className={ls.opIcon} url={appearance&&appearance.icon96} size={24} /> <ImgIcon className={ls.opIcon} url={appearance&&appearance.icon96} size={24} />
<span className={ls.opIndex}>{ index + 1 }</span> <span className={ls.opIndex}>{ modification.params.featureId }</span>
</div>; </div>;
}); });

View file

@ -43,12 +43,11 @@ function SelectedModificationInfo({ history, index,
} }
const appearance = resolveAppearance(op, m.params); const appearance = resolveAppearance(op, m.params);
const indexNumber = index + 1;
return <Widget visible={visible} return <Widget visible={visible}
left={lh && lh.x} left={lh && lh.x}
bottom={95} bottom={95}
flatRight={!lh} flatRight={!lh}
title={appearance.label.toUpperCase() + ' operation #' + indexNumber} title={appearance.label.toUpperCase() + ' operation #' + m.params.featureId}
onClose={close}> onClose={close}>
<div className={ls.requestInfo}> <div className={ls.requestInfo}>
<ImgIcon className={ls.pic} url={appearance && appearance.icon96} size={48}/> <ImgIcon className={ls.pic} url={appearance && appearance.icon96} size={48}/>

View file

@ -2,7 +2,7 @@ import {setSketchPrecision} from './sketch/sketchReader';
import {runSandbox} from './sandbox'; import {runSandbox} from './sandbox';
import {LOG_FLAGS} from './logFlags'; import {LOG_FLAGS} from './logFlags';
import {ApplicationContext} from "cad/context"; import {ApplicationContext} from "cad/context";
import {ProjectModel} from "./projectManager/projectManagerBundle"; import {ProjectCounters, ProjectModel} from "./projectManager/projectManagerBundle";
import {DebugMode$} from "debugger/Debugger"; import {DebugMode$} from "debugger/Debugger";
import {fillUpMissingFields} from "cad/craft/schema/initializeBySchema"; 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) { if (!currentWorkbench?.internal && ctx.workbenchService.defaultWorkbenchId !== currentWorkbench.workbenchId) {
data.workbench = currentWorkbench.workbenchId; data.workbench = currentWorkbench.workbenchId;
} }
data.counters = counterGenerator.projectCounters;
ctx.storageService.set(projectStorageKey(), JSON.stringify(data)); 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); const operation = ctx.operationService.get(req.type);
if (operation) { if (operation) {
fillUpMissingFields(req.params, operation.schema, ctx); 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) { function loadWorkbench(data: ProjectModel) {
@ -99,6 +106,7 @@ export function initProjectService(ctx: ApplicationContext, id: string, hints: a
ctx.assemblyService.loadConstraints(data.assembly); ctx.assemblyService.loadConstraints(data.assembly);
} }
counterGenerator = new ProjectCounterGenerator(data.counters);
} }
function empty() { function empty() {
@ -108,9 +116,11 @@ export function initProjectService(ctx: ApplicationContext, id: string, hints: a
}); });
} }
let counterGenerator = new ProjectCounterGenerator(new ProjectCounters());
ctx.projectService = { ctx.projectService = {
id, sketchStorageKey, projectStorageKey, sketchStorageNamespace, getSketchURL, save, load, loadData, empty, 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 { export interface ProjectService {
readonly id: string; readonly id: string;
@ -183,6 +206,7 @@ export interface ProjectService {
empty(): void; empty(): void;
counterGenerator: ProjectCounterGenerator;
} }
export interface ProjectBundleContext { export interface ProjectBundleContext {

View file

@ -197,6 +197,10 @@ export function activate(ctx: ApplicationContext) {
ctx.services.projectManager = ctx.projectManager; ctx.services.projectManager = ctx.projectManager;
} }
export class ProjectCounters {
featureId: number = 0;
}
export interface ProjectModel { export interface ProjectModel {
history: OperationRequest[], history: OperationRequest[],
@ -205,7 +209,9 @@ export interface ProjectModel {
assembly?: AssemblyConstraintDefinition[]; assembly?: AssemblyConstraintDefinition[];
workbench?: string workbench?: string,
counters?: ProjectCounters;
} }
export interface ModelBundle { export interface ModelBundle {