Skip to content

Commit

Permalink
Refactor DmnEditorRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
danielzhe committed Sep 12, 2024
1 parent e22fcd1 commit 3cb35a5
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions packages/dmn-editor-envelope/src/DmnEditorRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class DmnEditorRoot extends React.Component<DmnEditorRootProps, DmnEditor
// Save stack
let savedStackPointer: Normalized<DmnLatestModel>[] = [];

// Set the model and path for external model's manager.
// Set the model and path for external models manager.
this.setState((prev) => {
savedStackPointer = [...prev.stack];
return {
Expand All @@ -160,7 +160,7 @@ export class DmnEditorRoot extends React.Component<DmnEditorRootProps, DmnEditor
const newStack = savedStackPointer.slice(0, prev.pointer + 1);
return {
marshaller,
openFileNormalizedPosixPathRelativeToTheWorkspaceRoot: openFileNormalizedPosixPathRelativeToTheWorkspaceRoot,
openFileNormalizedPosixPathRelativeToTheWorkspaceRoot,
stack: [...newStack, normalize(marshaller.parser.parse())],
isReadOnly: prev.isReadOnly,
pointer: newStack.length,
Expand All @@ -172,7 +172,7 @@ export class DmnEditorRoot extends React.Component<DmnEditorRootProps, DmnEditor
else {
return {
marshaller,
openFileNormalizedPosixPathRelativeToTheWorkspaceRoot: openFileNormalizedPosixPathRelativeToTheWorkspaceRoot,
openFileNormalizedPosixPathRelativeToTheWorkspaceRoot,
stack: [normalize(marshaller.parser.parse())],
isReadOnly: prev.isReadOnly,
pointer: 0,
Expand Down Expand Up @@ -647,16 +647,16 @@ function ExternalModelsManager({
.documentElement.getAttribute("namespace");

if (namespaceOfTheResourceFile && namespacesSet.has(namespaceOfTheResourceFile)) {
checkIfNamespaceIsAlreadyLoaded(
checkIfNamespaceIsAlreadyLoaded({
externalModelsIndex,
namespaceOfTheResourceFile,
resource.normalizedPosixPathRelativeToTheWorkspaceRoot
);
normalizedPosixPathRelativeToTheWorkspaceRoot: resource.normalizedPosixPathRelativeToTheWorkspaceRoot,
});

loadModel({
includedModelContent: resourceContent,
includedNamespace: namespaceOfTheResourceFile,
externalModelsIndex: externalModelsIndex,
includedModelNamespace: namespaceOfTheResourceFile,
externalModelsIndex,
thisDmnsNormalizedPosixPathRelativeToTheWorkspaceRoot,
loadedDmnsByPathRelativeToTheWorkspaceRoot,
normalizedPosixPathRelativeToTheWorkspaceRoot: resource.normalizedPosixPathRelativeToTheWorkspaceRoot,
Expand Down Expand Up @@ -730,10 +730,10 @@ function getIncludedNamespacesFromModel(imports: Normalized<DmnLatestModel["defi
function loadModel(args: {
thisDmnsNormalizedPosixPathRelativeToTheWorkspaceRoot: string;
resourcesByNamespace: Map<string, ResourceContent>;
normalizedPosixPathRelativeToTheWorkspaceRoot: any;
includedNamespace: any;
normalizedPosixPathRelativeToTheWorkspaceRoot: string;
includedModelNamespace: string;
loadedDmnsByPathRelativeToTheWorkspaceRoot: Set<string>;
includedModelContent: any;
includedModelContent: string;
externalModelsIndex: DmnEditor.ExternalModelsIndex;
}) {
const normalizedPosixPathRelativeToTheOpenFile = __path.relative(
Expand All @@ -742,7 +742,7 @@ function loadModel(args: {
);

const includedModel = normalize(getMarshaller(args.includedModelContent, { upgradeTo: "latest" }).parser.parse());
args.externalModelsIndex[args.includedNamespace] = {
args.externalModelsIndex[args.includedModelNamespace] = {
normalizedPosixPathRelativeToTheOpenFile,
model: includedModel,
type: "dmn",
Expand All @@ -751,69 +751,63 @@ function loadModel(args: {

args.loadedDmnsByPathRelativeToTheWorkspaceRoot.add(args.normalizedPosixPathRelativeToTheWorkspaceRoot);

loadDependentModels(
includedModel,
args.externalModelsIndex,
args.resourcesByNamespace,
args.loadedDmnsByPathRelativeToTheWorkspaceRoot,
args.thisDmnsNormalizedPosixPathRelativeToTheWorkspaceRoot
);
loadDependentModels({
...args,
model: includedModel,
});
}

// Load all included models from the model and the included models of those models, recursively.
function loadDependentModels(
model: Normalized<DmnLatestModel>,
externalModelsIndex: DmnEditor.ExternalModelsIndex,
resourcesByNamespace: Map<string, ResourceContent>,
loadedDmnsByPathRelativeToTheWorkspaceRoot: Set<string>,
thisDmnsNormalizedPosixPathRelativeToTheWorkspaceRoot: string
) {
function loadDependentModels(args: {
model: Normalized<DmnLatestModel>;
externalModelsIndex: DmnEditor.ExternalModelsIndex;
resourcesByNamespace: Map<string, ResourceContent>;
loadedDmnsByPathRelativeToTheWorkspaceRoot: Set<string>;
thisDmnsNormalizedPosixPathRelativeToTheWorkspaceRoot: string;
}) {
const includedNamespaces = new Set(
getIncludedNamespacesFromModel(model.definitions.import).split(NAMESPACES_EFFECT_SEPARATOR)
getIncludedNamespacesFromModel(args.model.definitions.import).split(NAMESPACES_EFFECT_SEPARATOR)
);

for (const includedNamespace of includedNamespaces) {
if (!resourcesByNamespace.has(includedNamespace)) {
if (!args.resourcesByNamespace.has(includedNamespace)) {
console.warn(
`DMN EDITOR ROOT: The included namespace '${includedNamespace}' for the model '${model.definitions["@_id"]}' can not be found.`
`DMN EDITOR ROOT: The included namespace '${includedNamespace}' for the model '${args.model.definitions["@_id"]}' can not be found.`
);
continue;
}

const resource = resourcesByNamespace.get(includedNamespace)!;
if (loadedDmnsByPathRelativeToTheWorkspaceRoot.has(resource.normalizedPosixPathRelativeToTheWorkspaceRoot)) {
const resource = args.resourcesByNamespace.get(includedNamespace)!;
if (args.loadedDmnsByPathRelativeToTheWorkspaceRoot.has(resource.normalizedPosixPathRelativeToTheWorkspaceRoot)) {
continue;
}

checkIfNamespaceIsAlreadyLoaded(
externalModelsIndex,
includedNamespace,
resource.normalizedPosixPathRelativeToTheWorkspaceRoot
);
checkIfNamespaceIsAlreadyLoaded({
externalModelsIndex: args.externalModelsIndex,
namespaceOfTheResourceFile: includedNamespace,
normalizedPosixPathRelativeToTheWorkspaceRoot: resource.normalizedPosixPathRelativeToTheWorkspaceRoot,
});

loadModel({
thisDmnsNormalizedPosixPathRelativeToTheWorkspaceRoot,
...args,
includedModelContent: resource.content ?? "",
normalizedPosixPathRelativeToTheWorkspaceRoot: resource.normalizedPosixPathRelativeToTheWorkspaceRoot,
externalModelsIndex,
includedNamespace,
loadedDmnsByPathRelativeToTheWorkspaceRoot,
resourcesByNamespace,
includedModelNamespace: includedNamespace,
});
}
}

function checkIfNamespaceIsAlreadyLoaded(
externalModelsIndex: DmnEditor.ExternalModelsIndex,
namespaceOfTheResourceFile: string,
normalizedPosixPathRelativeToTheWorkspaceRoot: string
) {
if (externalModelsIndex[namespaceOfTheResourceFile]) {
function checkIfNamespaceIsAlreadyLoaded(args: {
externalModelsIndex: DmnEditor.ExternalModelsIndex;
namespaceOfTheResourceFile: string;
normalizedPosixPathRelativeToTheWorkspaceRoot: string;
}) {
if (args.externalModelsIndex[args.namespaceOfTheResourceFile]) {
console.warn(
`DMN EDITOR ROOT: Multiple DMN models encountered with the same namespace '${namespaceOfTheResourceFile}': '${
normalizedPosixPathRelativeToTheWorkspaceRoot
`DMN EDITOR ROOT: Multiple DMN models encountered with the same namespace '${args.namespaceOfTheResourceFile}': '${
args.normalizedPosixPathRelativeToTheWorkspaceRoot
}' and '${
externalModelsIndex[namespaceOfTheResourceFile]!.normalizedPosixPathRelativeToTheOpenFile
args.externalModelsIndex[args.namespaceOfTheResourceFile]!.normalizedPosixPathRelativeToTheOpenFile
}'. The latter will be considered.`
);
}
Expand Down

0 comments on commit 3cb35a5

Please sign in to comment.