Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Unedited markdown widgets not persisting #2019

Merged
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
35 changes: 3 additions & 32 deletions packages/dashboard-core-plugins/src/MarkdownPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { nanoid } from 'nanoid';
import {
assertIsDashboardPluginProps,
DashboardPluginComponentProps,
dehydrate,
hydrate,
LayoutUtils,
useListener,
} from '@deephaven/dashboard';
Expand All @@ -21,24 +19,6 @@ export type MarkdownComponentState = {
export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
assertIsDashboardPluginProps(props);
const { id, layout, panelManager, registerComponent } = props;
const dehydrateMarkdown = useCallback(config => {
const { title, componentState, props: configProps } = config;
let { panelState = null }: MarkdownComponentState = configProps;
if (componentState != null) {
({ panelState = null } = componentState as MarkdownComponentState);
}
if (
title == null ||
panelState == null ||
panelState.content == null ||
panelState.content.length === 0 ||
panelState.content === MarkdownUtils.DEFAULT_CONTENT
) {
// We don't want to save it if there's no content
return null;
}
return dehydrate(config);
}, []);

const handleOpen = useCallback(
({
Expand All @@ -52,16 +32,12 @@ export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
const openedMarkdowns = panelManager.getOpenedPanelConfigsOfType(
MarkdownPanel.COMPONENT
);
const closedMarkdowns = panelManager.getClosedPanelConfigsOfType(
MarkdownPanel.COMPONENT
);
const usedTitles = openedMarkdowns.map(markdown => markdown.title ?? '');
const panelTitle =
title != null && title !== ''
? title
: MarkdownUtils.getNewMarkdownTitle(usedTitles);
const content =
closedMarkdowns.length > 0 ? null : MarkdownUtils.DEFAULT_CONTENT;
const content = null;
const config = {
type: 'react-component' as const,
component: MarkdownPanel.COMPONENT,
Expand Down Expand Up @@ -90,19 +66,14 @@ export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
useEffect(
function registerComponentsAndReturnCleanup() {
const cleanups = [
registerComponent(
MarkdownPanel.COMPONENT,
MarkdownPanel,
hydrate,
dehydrateMarkdown
),
registerComponent(MarkdownPanel.COMPONENT, MarkdownPanel),
];

return () => {
cleanups.forEach(cleanup => cleanup());
};
},
[dehydrateMarkdown, registerComponent]
[registerComponent]
);

useListener(layout.eventHub, MarkdownEvent.OPEN, handleOpen);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ClosedPanels } from '@deephaven/dashboard';
import memoize from 'memoize-one';

class MarkdownUtils {
static DEFAULT_TITLE = 'Note';

Expand All @@ -19,6 +22,17 @@ class MarkdownUtils {

return title;
}

static getClosedMarkdowns = memoize((closedPanels: ClosedPanels) =>
closedPanels
.filter(
panel =>
panel.component === 'MarkdownPanel' &&
panel.props.panelState.content !== '' &&
panel.props.panelState.content !== null
)
.reverse()
);
}

export default MarkdownUtils;
18 changes: 11 additions & 7 deletions packages/dashboard-core-plugins/src/panels/MarkdownPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import memoize from 'memoize-one';
import { connect } from 'react-redux';
import {
ClosedPanel,
ClosedPanels,
DashboardPanelProps,
getClosedPanelsForDashboard,
LayoutUtils,
Expand All @@ -27,6 +26,7 @@ import MarkdownContainer from '../controls/markdown/MarkdownContainer';
import MarkdownStartPage from '../controls/markdown/MarkdownStartPage';
import './MarkdownPanel.scss';
import type MarkdownEditorType from '../controls/markdown/MarkdownEditor';
import MarkdownUtils from '../controls/markdown/MarkdownUtils';

const MarkdownEditor = lazy(
() => import('../controls/markdown/MarkdownEditor')
Expand Down Expand Up @@ -77,11 +77,17 @@ export class MarkdownPanel extends Component<
const { panelState } = props;
let content = null;
if (panelState != null && panelState.content != null) {
({ content } = panelState);
if (panelState.content !== '') {
({ content } = panelState);
} else {
content = MarkdownUtils.DEFAULT_CONTENT;
}
}

this.state = {
isStartPageShown: content == null,
isStartPageShown:
content == null &&
this.getClosedMarkdowns(props.closedPanels).length !== 0,
isEditing: false,
content,

Expand Down Expand Up @@ -123,9 +129,7 @@ export class MarkdownPanel extends Component<
}
}

getClosedMarkdowns = memoize((closedPanels: ClosedPanels) =>
closedPanels.filter(panel => panel.component === 'MarkdownPanel').reverse()
);
getClosedMarkdowns = memoize(MarkdownUtils.getClosedMarkdowns);

handleContainerDoubleClick(event: MouseEvent<Element>): void {
const { isEditing } = this.state;
Expand Down Expand Up @@ -242,7 +246,7 @@ export class MarkdownPanel extends Component<
this.markdownEditor = markdownEditor;
}}
isEditing={isEditing}
content={content ?? undefined}
content={content ?? MarkdownUtils.DEFAULT_CONTENT}
onEditorInitialized={this.handleEditorInitialized}
/>
</Suspense>
Expand Down
8 changes: 5 additions & 3 deletions packages/dashboard/src/PanelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,11 @@ class PanelManager {
config.component,
config
);
(dehydratedConfig as ClosedPanel).parentStackId =
LayoutUtils.getStackForConfig(root, config)?.config.id;
this.closed.push(dehydratedConfig);
if (dehydratedConfig != null) {
(dehydratedConfig as ClosedPanel).parentStackId =
LayoutUtils.getStackForConfig(root, config)?.config.id;
this.closed.push(dehydratedConfig);
}
}
}

Expand Down
Loading