Skip to content

Commit bb5ef5b

Browse files
Reworked node auto resizing
1 parent 245c6d1 commit bb5ef5b

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/@types/Canvas.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export interface CanvasNodeData {
210210
// TODO: needsToBeInitialized?: boolean
211211
styleAttributes?: { [key: string]: string | null }
212212

213-
lockedHeight?: boolean
213+
autoResizeHeight?: boolean
214214

215215
isCollapsed?: boolean
216216
collapsedData?: CanvasData

src/canvas-extensions/auto-resize-node-canvas-extension.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,45 @@ export default class AutoResizeNodeCanvasExtension extends CanvasExtension {
3333
}) as CanvasNode[]
3434
if (selectedNodes.length === 0) return
3535

36-
const hasLockedHeight = selectedNodes.some(node => node.getData().lockedHeight)
36+
const autoResizeHeightEnabled = selectedNodes.some(node => node.getData().autoResizeHeight)
3737

3838
CanvasHelper.addPopupMenuOption(
3939
canvas,
4040
CanvasHelper.createPopupMenuOption({
41-
id: 'lock-height',
42-
label: 'Toggle locked height',
43-
icon: hasLockedHeight ? 'ruler' : 'pencil-ruler',
44-
callback: () => this.setLockedHeight(canvas, selectedNodes, hasLockedHeight)
41+
id: 'auto-resize-height',
42+
label: autoResizeHeightEnabled ? 'Disable auto-resize' : 'Enable auto-resize',
43+
icon: autoResizeHeightEnabled ? 'scan-text' : 'lock',
44+
callback: () => this.toggleAutoResizeHeightEnabled(canvas, selectedNodes, autoResizeHeightEnabled)
4545
})
4646
)
4747
}
4848

49-
private setLockedHeight(canvas: Canvas, nodes: CanvasNode[], lockedHeight: boolean) {
50-
const newLockedHeight = lockedHeight ? undefined : true
49+
private toggleAutoResizeHeightEnabled(canvas: Canvas, nodes: CanvasNode[], autoResizeHeight: boolean) {
50+
const newAutoResizeHeight = autoResizeHeight ? undefined : true
5151

5252
nodes.forEach(node => node.setData({
5353
...node.getData(),
54-
lockedHeight: newLockedHeight
54+
autoResizeHeight: newAutoResizeHeight
5555
}))
5656

5757
this.onPopupMenuCreated(canvas)
5858
}
5959

60+
private canBeResized(node: CanvasNode) {
61+
const nodeData = node.getData()
62+
return nodeData.autoResizeHeight
63+
}
64+
6065
private async onNodeEditingStateChanged(_canvas: Canvas, node: CanvasNode, editing: boolean) {
66+
if (!this.canBeResized(node)) return
67+
6168
await sleep(10)
6269

6370
if (editing) {
6471
this.onNodeTextContentChanged(_canvas, node, node.child.editMode.cm.dom)
6572
return
6673
}
6774

68-
const nodeData = node.getData()
69-
if (nodeData.lockedHeight) return
70-
7175
const renderedMarkdownContainer = node.nodeEl.querySelector(".markdown-preview-view.markdown-rendered") as HTMLElement | null
7276
if (!renderedMarkdownContainer) return
7377

@@ -79,8 +83,7 @@ export default class AutoResizeNodeCanvasExtension extends CanvasExtension {
7983
}
8084

8185
private async onNodeTextContentChanged(_canvas: Canvas, node: CanvasNode, dom: HTMLElement) {
82-
const nodeData = node.getData()
83-
if (nodeData.lockedHeight) return
86+
if (!this.canBeResized(node)) return
8487

8588
const cmScroller = dom.querySelector(".cm-scroller") as HTMLElement | null
8689
if (!cmScroller) return
@@ -94,6 +97,10 @@ export default class AutoResizeNodeCanvasExtension extends CanvasExtension {
9497

9598
private setNodeHeight(node: CanvasNode, height: number) {
9699
if (height === 0) return
100+
101+
// Limit the height to the maximum allowed
102+
const maxHeight = this.plugin.settings.getSetting('autoResizeNodeMaxHeight')
103+
if (maxHeight != -1 && height > maxHeight) height = maxHeight
97104

98105
const nodeData = node.getData()
99106

src/settings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface AdvancedCanvasPluginSettingsValues {
4949
disablePan: boolean
5050

5151
autoResizeNodeFeatureEnabled: boolean
52+
autoResizeNodeMaxHeight: number
5253
autoResizeNodeSnapToGrid: boolean
5354

5455
collapsibleGroupsFeatureEnabled: boolean
@@ -262,6 +263,12 @@ export const SETTINGS = {
262263
label: 'Auto resize node',
263264
description: 'Automatically resize the height of a node to fit the content.',
264265
children: {
266+
autoResizeNodeMaxHeight: {
267+
label: 'Max height',
268+
description: 'The maximum height of the node when auto resizing (-1 for unlimited).',
269+
type: 'number',
270+
parse: (value: string) => Math.max(-1, parseInt(value) ?? -1)
271+
},
265272
autoResizeNodeSnapToGrid: {
266273
label: 'Snap to grid',
267274
description: 'When enabled, the height of the node will snap to the grid.',
@@ -403,6 +410,7 @@ export const DEFAULT_SETTINGS_VALUES: AdvancedCanvasPluginSettingsValues = {
403410
disablePan: false,
404411

405412
autoResizeNodeFeatureEnabled: false,
413+
autoResizeNodeMaxHeight: -1,
406414
autoResizeNodeSnapToGrid: true,
407415

408416
collapsibleGroupsFeatureEnabled: true,

0 commit comments

Comments
 (0)