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);
}
function get<T>(id: string): Operation<T> {
function get<T extends FeatureIdParam>(id: string): Operation<T> {
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<R> extends OperationDescriptor<R>{
interface FeatureIdParam {
featureId: string;
}
export interface Operation<R extends FeatureIdParam> extends OperationDescriptor<R>{
appearance: {
id: string;
label: string;
@ -133,7 +137,7 @@ type OpIcon = IconDeclaration | IconType | string | ((props: any) => JSX.Element
type MaterializedParams = any;
export interface OperationDescriptor<R> {
export interface OperationDescriptor<R extends FeatureIdParam> {
id: string;
label: string;
info: string;
@ -162,7 +166,7 @@ export interface OperationDescriptor<R> {
export interface OperationService {
registerOperations(descriptior: OperationDescriptor<any>[]);
get<T>(operationId: string): Operation<T>;
get<T extends FeatureIdParam>(operationId: string): Operation<T>;
}
export type Index<T> = {

View file

@ -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;

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)}
onClick={e => toggle(index, modification, e.currentTarget)}>
<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>;
});

View file

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

View file

@ -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 {