FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: use the right list of excluded fields in GraphCodec by tbouffard · Pull Request #777 · maxGraph/maxGraph · GitHub

forked from jgraph/mxgraph
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (8) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2024-present The maxGraph project Contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { afterEach, beforeAll, beforeEach, expect, test } from '@jest/globals';
import { createGraphWithoutContainer } from '../../utils';
import Codec from '../../../src/serialization/Codec';
import { getPrettyXml, parseXml } from '../../../src/util/xmlUtils';
import {
type AbstractGraph,
ImageBox,
Rectangle,
registerCoreCodecs,
unregisterAllCodecs,
} from '../../../src';

function exportGraph(graph: AbstractGraph): string {
const encodedNode = new Codec().encode(graph);
return getPrettyXml(encodedNode);
}

function importGraph(graph: AbstractGraph, input: string): void {
const doc = parseXml(input);
new Codec(doc).decode(doc.documentElement, graph);
}

// Prevents side effects between tests
beforeAll(() => {
unregisterAllCodecs();
});
beforeEach(() => {
registerCoreCodecs();
});
afterEach(() => {
unregisterAllCodecs();
});

const graphAsXml = `<Graph>
<Array as="cells" />
<Array as="imageBundles" />
<Array as="mouseListeners">
<Object />
<Object />
</Array>
<Array as="multiplicities" />
<GraphDataModel as="model">
<root>
<Cell id="0">
<Object as="style" />
</Cell>
<Cell id="1" parent="0">
<Object as="style" />
</Cell>
</root>
</GraphDataModel>
<Stylesheet as="stylesheet" />
<Rectangle _x="123" _y="453" _width="60" _height="60" as="pageFormat" />
<ImageBox src="./warning.gif" width="16" height="16" as="warningImage" />
<Object foldingEnabled="1" collapseToPreferredSize="1" as="options">
<ImageBox src="./collapsed-new.gif" width="10" height="10" as="collapsedImage" />
<ImageBox src="./expanded.gif" width="9" height="9" as="expandedImage" />
</Object>
</Graph>
`;

test('Export Graph with default plugins', () => {
// This graph uses default plugins
const graph = createGraphWithoutContainer();
// override defaults to ensure it is taken into account
graph.pageFormat = new Rectangle(123, 453, 60, 60);
graph.options.collapsedImage = new ImageBox('./collapsed-new.gif', 10, 10);

expect(exportGraph(graph)).toBe(graphAsXml);
});

test('Import Graph', () => {
// This graph uses default plugins
const graph = createGraphWithoutContainer();
// check default values that will be overridden by the import
expect(graph.pageFormat).toEqual(new Rectangle(0, 0, 827, 1169));
expect(graph.options.collapsedImage).toEqual(new ImageBox('./collapsed.gif', 9, 9));

importGraph(graph, graphAsXml);

// new values due to import
expect(graph.pageFormat).toEqual(new Rectangle(123, 453, 60, 60));
expect(graph.options.collapsedImage).toEqual(
new ImageBox('./collapsed-new.gif', 10, 10)
);
});
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { describe, test } from '@jest/globals';
import { afterEach, beforeAll, describe, test } from '@jest/globals';
import { ModelChecker } from './utils';
import { Geometry, GraphDataModel, ModelXmlSerializer, Point } from '../../src';
import {
Geometry,
GraphDataModel,
ModelXmlSerializer,
Point,
unregisterAllCodecs,
} from '../../src';

// Prevents side effects between tests
beforeAll(() => {
unregisterAllCodecs();
});
afterEach(() => {
unregisterAllCodecs();
});

describe('import mxGraph model', () => {
test('Model with geometry', () => {
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { describe, expect, test } from '@jest/globals';
import { afterEach, beforeAll, describe, expect, test } from '@jest/globals';
import { ModelChecker } from './utils';
import { createGraphWithoutContainer } from '../utils';
import {
Expand All @@ -24,6 +24,7 @@ import {
GraphDataModel,
ModelXmlSerializer,
Point,
unregisterAllCodecs,
} from '../../src';

// inspired by VertexMixin.createVertex
Expand Down Expand Up @@ -97,6 +98,14 @@ const xmlWithVerticesAndEdges = `<GraphDataModel>
</GraphDataModel>
`;

// Prevents side effects between tests
beforeAll(() => {
unregisterAllCodecs();
});
afterEach(() => {
unregisterAllCodecs();
});

test('Check the content of an empty GraphDataModel', () => {
const modelChecker = new ModelChecker(new GraphDataModel());
// Ensure that we have the same content as after an import
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/serialization/codecs/GraphCodec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ import { Graph } from '../../view/Graph';
* - container
* - cellRenderer
* - editor
* - selection
* - selectionModel
* - plugins
*
* @category Serialization with Codecs
*/
export class GraphCodec extends ObjectCodec {
constructor() {
// TODO review the Graph initialization. Currently it registers all default plugins (check impact on tree-shaking)
super(new Graph(), [
// Do not load default plugins, plugins are not serialized
super(new Graph(undefined, undefined, []), [
'graphListeners',
'eventListeners',
'view',
'container',
'cellRenderer',
'editor',
'selection',
'selectionModel',
'plugins',
]);
this.setName('Graph');
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ import Point from '../../view/geometry/Point';
*/
export class GraphViewCodec extends ObjectCodec {
constructor() {
const __dummy: any = undefined;
super(new GraphView(__dummy));
super(new GraphView(undefined!));
this.setName('GraphView');
}

/**
* Encodes the given {@link GraphView} using {@link encodeCell} starting at the model's root. This returns the
* top-level graph node of the recursive encoding.
*/
encode(enc: any, view: GraphView) {
return this.encodeCell(enc, view, <Cell>view.graph.getDataModel().getRoot());
return this.encodeCell(enc, view, view.graph.getDataModel().getRoot()!);
}

/**
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ import {
} from './codecs/_model-codecs';
import Geometry from '../view/geometry/Geometry';
import Point from '../view/geometry/Point';
import ObjectCodec from './ObjectCodec';
import { CodecRegistrationStates, registerBaseCodecs } from './register-shared';

const createObjectCodec = (template: any, name: string): ObjectCodec => {
const objectCodec = new ObjectCodec(template);
objectCodec.setName(name);
return objectCodec;
};
import {
CodecRegistrationStates,
createObjectCodec,
registerBaseCodecs,
} from './register-shared';

/**
* Register model codecs i.e. codecs used to import/export the Graph Model, see {@link GraphDataModel}.
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ import {
StylesheetCodec,
TerminalChangeCodec,
} from './codecs/_other-codecs';
import Rectangle from '../view/geometry/Rectangle';
import ImageBox from '../view/image/ImageBox';
import CellAttributeChange from '../view/undoable_changes/CellAttributeChange';
import CollapseChange from '../view/undoable_changes/CollapseChange';
import GeometryChange from '../view/undoable_changes/GeometryChange';
import StyleChange from '../view/undoable_changes/StyleChange';
import ValueChange from '../view/undoable_changes/ValueChange';
import VisibleChange from '../view/undoable_changes/VisibleChange';
import { CodecRegistrationStates, registerBaseCodecs } from './register-shared';
import {
CodecRegistrationStates,
createObjectCodec,
registerBaseCodecs,
} from './register-shared';
import { registerModelCodecs } from './register-model-codecs';

const registerGenericChangeCodecs = () => {
Expand Down Expand Up @@ -78,6 +84,10 @@ export const registerCoreCodecs = (force = false) => {
CodecRegistry.register(new TerminalChangeCodec());
registerGenericChangeCodecs();

// Needed at least by Graph
CodecRegistry.register(createObjectCodec(new Rectangle(), 'Rectangle'));
CodecRegistry.register(createObjectCodec(new ImageBox(undefined!, 0, 0), 'ImageBox'));

registerModelCodecs(force);

CodecRegistrationStates.core = true;
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/serialization/register-shared.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ export const registerBaseCodecs = (force = false) => {
CodecRegistrationStates.base = true;
}
};

export const createObjectCodec = (template: any, name: string): ObjectCodec => {
const objectCodec = new ObjectCodec(template);
objectCodec.setName(name);
return objectCodec;
};

Back | FazBrowse Home | New Git URL