Skip to content

Commit c17f136

Browse files
authored
fix: Unedited markdown widgets not persisting (#2019)
Resolves #1777 Changes Implemented: - Added checks, modified hydration logic, and added new conditions for what is considered a closed markdown, to ensure proper persistence of markdown widget
1 parent 3115dd1 commit c17f136

4 files changed

Lines changed: 33 additions & 42 deletions

File tree

packages/dashboard-core-plugins/src/MarkdownPlugin.tsx

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { nanoid } from 'nanoid';
33
import {
44
assertIsDashboardPluginProps,
55
DashboardPluginComponentProps,
6-
dehydrate,
7-
hydrate,
86
LayoutUtils,
97
useListener,
108
} from '@deephaven/dashboard';
@@ -21,24 +19,6 @@ export type MarkdownComponentState = {
2119
export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
2220
assertIsDashboardPluginProps(props);
2321
const { id, layout, panelManager, registerComponent } = props;
24-
const dehydrateMarkdown = useCallback(config => {
25-
const { title, componentState, props: configProps } = config;
26-
let { panelState = null }: MarkdownComponentState = configProps;
27-
if (componentState != null) {
28-
({ panelState = null } = componentState as MarkdownComponentState);
29-
}
30-
if (
31-
title == null ||
32-
panelState == null ||
33-
panelState.content == null ||
34-
panelState.content.length === 0 ||
35-
panelState.content === MarkdownUtils.DEFAULT_CONTENT
36-
) {
37-
// We don't want to save it if there's no content
38-
return null;
39-
}
40-
return dehydrate(config);
41-
}, []);
4222

4323
const handleOpen = useCallback(
4424
({
@@ -52,16 +32,12 @@ export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
5232
const openedMarkdowns = panelManager.getOpenedPanelConfigsOfType(
5333
MarkdownPanel.COMPONENT
5434
);
55-
const closedMarkdowns = panelManager.getClosedPanelConfigsOfType(
56-
MarkdownPanel.COMPONENT
57-
);
5835
const usedTitles = openedMarkdowns.map(markdown => markdown.title ?? '');
5936
const panelTitle =
6037
title != null && title !== ''
6138
? title
6239
: MarkdownUtils.getNewMarkdownTitle(usedTitles);
63-
const content =
64-
closedMarkdowns.length > 0 ? null : MarkdownUtils.DEFAULT_CONTENT;
40+
const content = null;
6541
const config = {
6642
type: 'react-component' as const,
6743
component: MarkdownPanel.COMPONENT,
@@ -90,19 +66,14 @@ export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
9066
useEffect(
9167
function registerComponentsAndReturnCleanup() {
9268
const cleanups = [
93-
registerComponent(
94-
MarkdownPanel.COMPONENT,
95-
MarkdownPanel,
96-
hydrate,
97-
dehydrateMarkdown
98-
),
69+
registerComponent(MarkdownPanel.COMPONENT, MarkdownPanel),
9970
];
10071

10172
return () => {
10273
cleanups.forEach(cleanup => cleanup());
10374
};
10475
},
105-
[dehydrateMarkdown, registerComponent]
76+
[registerComponent]
10677
);
10778

10879
useListener(layout.eventHub, MarkdownEvent.OPEN, handleOpen);

packages/dashboard-core-plugins/src/controls/markdown/MarkdownUtils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { ClosedPanels } from '@deephaven/dashboard';
2+
import memoize from 'memoize-one';
3+
14
class MarkdownUtils {
25
static DEFAULT_TITLE = 'Note';
36

@@ -19,6 +22,17 @@ class MarkdownUtils {
1922

2023
return title;
2124
}
25+
26+
static getClosedMarkdowns = memoize((closedPanels: ClosedPanels) =>
27+
closedPanels
28+
.filter(
29+
panel =>
30+
panel.component === 'MarkdownPanel' &&
31+
panel.props.panelState.content !== '' &&
32+
panel.props.panelState.content !== null
33+
)
34+
.reverse()
35+
);
2236
}
2337

2438
export default MarkdownUtils;

packages/dashboard-core-plugins/src/panels/MarkdownPanel.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import memoize from 'memoize-one';
1010
import { connect } from 'react-redux';
1111
import {
1212
ClosedPanel,
13-
ClosedPanels,
1413
DashboardPanelProps,
1514
getClosedPanelsForDashboard,
1615
LayoutUtils,
@@ -27,6 +26,7 @@ import MarkdownContainer from '../controls/markdown/MarkdownContainer';
2726
import MarkdownStartPage from '../controls/markdown/MarkdownStartPage';
2827
import './MarkdownPanel.scss';
2928
import type MarkdownEditorType from '../controls/markdown/MarkdownEditor';
29+
import MarkdownUtils from '../controls/markdown/MarkdownUtils';
3030

3131
const MarkdownEditor = lazy(
3232
() => import('../controls/markdown/MarkdownEditor')
@@ -77,11 +77,17 @@ export class MarkdownPanel extends Component<
7777
const { panelState } = props;
7878
let content = null;
7979
if (panelState != null && panelState.content != null) {
80-
({ content } = panelState);
80+
if (panelState.content !== '') {
81+
({ content } = panelState);
82+
} else {
83+
content = MarkdownUtils.DEFAULT_CONTENT;
84+
}
8185
}
8286

8387
this.state = {
84-
isStartPageShown: content == null,
88+
isStartPageShown:
89+
content == null &&
90+
this.getClosedMarkdowns(props.closedPanels).length !== 0,
8591
isEditing: false,
8692
content,
8793

@@ -123,9 +129,7 @@ export class MarkdownPanel extends Component<
123129
}
124130
}
125131

126-
getClosedMarkdowns = memoize((closedPanels: ClosedPanels) =>
127-
closedPanels.filter(panel => panel.component === 'MarkdownPanel').reverse()
128-
);
132+
getClosedMarkdowns = memoize(MarkdownUtils.getClosedMarkdowns);
129133

130134
handleContainerDoubleClick(event: MouseEvent<Element>): void {
131135
const { isEditing } = this.state;
@@ -242,7 +246,7 @@ export class MarkdownPanel extends Component<
242246
this.markdownEditor = markdownEditor;
243247
}}
244248
isEditing={isEditing}
245-
content={content ?? undefined}
249+
content={content ?? MarkdownUtils.DEFAULT_CONTENT}
246250
onEditorInitialized={this.handleEditorInitialized}
247251
/>
248252
</Suspense>

packages/dashboard/src/PanelManager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,11 @@ class PanelManager {
362362
config.component,
363363
config
364364
);
365-
(dehydratedConfig as ClosedPanel).parentStackId =
366-
LayoutUtils.getStackForConfig(root, config)?.config.id;
367-
this.closed.push(dehydratedConfig);
365+
if (dehydratedConfig != null) {
366+
(dehydratedConfig as ClosedPanel).parentStackId =
367+
LayoutUtils.getStackForConfig(root, config)?.config.id;
368+
this.closed.push(dehydratedConfig);
369+
}
368370
}
369371
}
370372

0 commit comments

Comments
 (0)