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
59 changes: 59 additions & 0 deletions packages/core/__tests__/view/mixins/EditingMixin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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 { describe, expect, jest, test } from '@jest/globals';
import { createGraphWithoutPlugins } from '../../utils';
import { Cell, type CellStateStyle } from '../../../src';

describe('isCellEditable', () => {
const createGraphMockingGetCurrentCellStyle = (cellIsEditable?: boolean) => {
const graph = createGraphWithoutPlugins();
graph.getCurrentCellStyle = jest
.fn<(cell: Cell, ignoreState?: boolean) => CellStateStyle>()
.mockReturnValue(cellIsEditable !== undefined ? { editable: cellIsEditable } : {});
return graph;
};

test('Using defaults', () => {
expect(
createGraphMockingGetCurrentCellStyle().isCellEditable(new Cell())
).toBeTruthy();
});

test('Using Cell with the "editable" style property set to "true"', () => {
expect(
createGraphMockingGetCurrentCellStyle(true).isCellEditable(new Cell())
).toBeTruthy();
});

test('Using Cell with the "editable" style property set to "false"', () => {
expect(
createGraphMockingGetCurrentCellStyle(false).isCellEditable(new Cell())
).toBeFalsy();
});

test('Cells not editable in Graph', () => {
const graph = createGraphWithoutPlugins();
graph.setCellsEditable(false);
expect(graph.isCellEditable(new Cell())).toBeFalsy();
});

test('Cells locked in Graph', () => {
const graph = createGraphWithoutPlugins();
graph.setCellsLocked(true);
expect(graph.isCellEditable(new Cell())).toBeFalsy();
});
});
3 changes: 2 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ export type CellStateStyle = {
edgeStyle?: StyleEdgeStyleValue;
/**
* This specifies if the value of a cell can be edited using the in-place editor.
* See {@link Graph.isCellEditable}.
*
* Note that a cell is in fact editable according to the value returned by {@link Graph.isCellEditable}.
* @default true
*/
editable?: boolean;
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/view/mixins/EditingMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ export const EditingMixin: PartialType = {
},

isCellEditable(cell): boolean {
const style = this.getCurrentCellStyle(cell);
return (
this.isCellsEditable() && !this.isCellLocked(cell) && (style.editable || false)
this.isCellsEditable() &&
!this.isCellLocked(cell) &&
(this.getCurrentCellStyle(cell).editable ?? true)
);
},

Expand Down