Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { afterEach, beforeAll, beforeEach, expect, test } from '@jest/globals';
import { afterEach, beforeAll, beforeEach, describe, 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,
BaseGraph,
getDefaultPlugins,
ImageBox,
Rectangle,
registerCoreCodecs,
Expand Down Expand Up @@ -47,7 +49,8 @@ afterEach(() => {
unregisterAllCodecs();
});

const graphAsXml = `<Graph>
function buildXml(name: string): string {
const xmlTemplate = `<@NAME@>
<Array as="cells" />
<Array as="imageBundles" />
<Array as="mouseListeners">
Expand All @@ -72,31 +75,40 @@ const graphAsXml = `<Graph>
<ImageBox src="./collapsed-new.gif" width="10" height="10" as="collapsedImage" />
<ImageBox src="./expanded.gif" width="9" height="9" as="expandedImage" />
</Object>
</Graph>
</@NAME@>
`;

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);
return xmlTemplate.replace(/@NAME@/g, name);
}

expect(exportGraph(graph)).toBe(graphAsXml);
});
describe.each([
[
'Graph',
() => createGraphWithoutContainer(), // This graph uses default plugins
],
['BaseGraph', () => new BaseGraph({ plugins: getDefaultPlugins() })],
])('%s', (name, graphFactory: () => AbstractGraph) => {
test('Export', () => {
const graph = graphFactory();
// 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(buildXml(name));
});

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));
test('Import', () => {
const graph = graphFactory();
// 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);
importGraph(graph, buildXml(name));

// 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)
);
// 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)
);
});
});
42 changes: 42 additions & 0 deletions packages/core/src/serialization/codecs/BaseGraphCodec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2025-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 { BaseGraph } from '../../view/BaseGraph';
import ObjectCodec from '../ObjectCodec';
import { excludedFields } from './GraphCodec';

/**
* Codec for {@link BaseGraph}s.
*
* Transient Fields:
*
* - graphListeners
* - eventListeners
* - view
* - container
* - cellRenderer
* - editor
* - selectionModel
* - plugins
*
* @category Serialization with Codecs
*/
export class BaseGraphCodec extends ObjectCodec {
constructor() {
super(new BaseGraph(), excludedFields);
this.setName('BaseGraph');
}
}
22 changes: 12 additions & 10 deletions packages/core/src/serialization/codecs/GraphCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ limitations under the License.
import ObjectCodec from '../ObjectCodec';
import { Graph } from '../../view/Graph';

export const excludedFields = [
'graphListeners',
'eventListeners',
'view',
'container',
'cellRenderer',
'editor',
'selectionModel',
'plugins',
];

/**
* Codec for {@link Graph}s.
* This class is created and registered dynamically at load time and used implicitly via {@link Codec} and the {@link CodecRegistry}.
Expand All @@ -39,16 +50,7 @@ import { Graph } from '../../view/Graph';
export class GraphCodec extends ObjectCodec {
constructor() {
// Do not load default plugins, plugins are not serialized
super(new Graph(undefined, undefined, []), [
'graphListeners',
'eventListeners',
'view',
'container',
'cellRenderer',
'editor',
'selectionModel',
'plugins',
]);
super(new Graph(undefined, undefined, []), excludedFields);
this.setName('Graph');
}
}
3 changes: 2 additions & 1 deletion packages/core/src/serialization/codecs/_other-codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ limitations under the License.
*/

export * from './editor';
export * from './BaseGraphCodec';
export * from './ChildChangeCodec';
export * from './GenericChangeCodec';
export * from './GraphCodec';
export { GraphCodec } from './GraphCodec';
export * from './GraphViewCodec';
export * from './RootChangeCodec';
export * from './StylesheetCodec';
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/serialization/register-other-codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

import CodecRegistry from './CodecRegistry';
import {
BaseGraphCodec,
ChildChangeCodec,
EditorCodec,
EditorKeyHandlerCodec,
Expand Down Expand Up @@ -77,6 +78,7 @@ const registerGenericChangeCodecs = () => {
export const registerCoreCodecs = (force = false) => {
if (!CodecRegistrationStates.core || force) {
CodecRegistry.register(new ChildChangeCodec());
CodecRegistry.register(new BaseGraphCodec());
CodecRegistry.register(new GraphCodec());
CodecRegistry.register(new GraphViewCodec());
CodecRegistry.register(new RootChangeCodec());
Expand Down