Skip to content

Commit 467dc06

Browse files
committed
refactor: use 5.3.0 destroy method
1 parent f223982 commit 467dc06

1 file changed

Lines changed: 22 additions & 36 deletions

File tree

src/widget.ts

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,14 @@ import type { IChangedTiddlers } from 'tiddlywiki';
66
import type { IReactWidget, ITWReactProps, ITWReactPropsDefault } from './widget-type';
77

88
import { widget as Widget } from '$:/core/modules/widgets/widget.js';
9-
type ReactType = typeof ReactType;
10-
type ReactDomType = typeof ReactDomType & typeof ReactDomClientType;
11-
const ReactDom: ReactDomType = require('react-dom');
12-
const React: ReactType = require('react');
9+
const ReactDom = require('react-dom') as typeof ReactDomType & typeof ReactDomClientType;
10+
const React = require('react') as typeof ReactType;
1311
if (typeof window !== 'undefined') {
1412
window.React = React;
1513
} else if (typeof global !== 'undefined') {
1614
global.React = React;
1715
}
1816

19-
// TODO: remove this hack after https://github.com/Jermolene/TiddlyWiki5/pull/6699 merged
20-
/*
21-
Remove any DOM nodes created by this widget or its children
22-
*/
23-
// @ts-expect-error Type '(parentRemoved: boolean) => void' is not assignable to type '() => void'.ts(2322)
24-
Widget.prototype.removeChildDomNodes = function(parentRemoved: boolean) {
25-
// If this widget has directly created DOM nodes, delete them and exit. This assumes that any child widgets are contained within the created DOM nodes, which would normally be the case
26-
// If parent has already detatch its dom node from the document, we don't need to do it again.
27-
if (this.domNodes.length > 0 && !parentRemoved) {
28-
$tw.utils.each(this.domNodes, function(domNode) {
29-
domNode?.parentNode?.removeChild(domNode);
30-
});
31-
this.domNodes = [];
32-
// inform child widget to do some custom cleanup in a overrided sub-class method, and tell child widget that parent has already done the update, so children don't need to do anything.
33-
parentRemoved = true;
34-
}
35-
// If parentRemoved is unset or false, will ask the child widgets to delete their DOM nodes
36-
$tw.utils.each(this.children, function(childWidget) {
37-
// @ts-expect-error Expected 0 arguments, but got 1.ts(2554)
38-
childWidget?.removeChildDomNodes(parentRemoved);
39-
});
40-
};
41-
4217
class ReactWidgetImpl<
4318
IProps extends ITWReactProps = ITWReactPropsDefault,
4419
> extends Widget implements IReactWidget<IProps> {
@@ -47,6 +22,7 @@ class ReactWidgetImpl<
4722

4823
refresh(changedTiddlers: IChangedTiddlers) {
4924
// we don't need to refresh mount point of react-dom
25+
// but you can override this method to do some custom refresh logic
5026
return false;
5127
}
5228

@@ -61,9 +37,6 @@ class ReactWidgetImpl<
6137

6238
getProps: () => IProps = () => ({ parentWidget: this } as unknown as IProps);
6339

64-
/**
65-
* Lifecycle method: Render this widget into the DOM
66-
*/
6740
render(parent: Element, nextSibling: Element | null) {
6841
this.parentDomNode = parent;
6942
this.computeAttributes();
@@ -89,14 +62,27 @@ class ReactWidgetImpl<
8962
}
9063

9164
refreshSelf() {
92-
const nextSibling = this.findNextSiblingDomNode();
93-
/** don't unmount root if we are just refresh tiddler, not closing it */
94-
// this.removeChildDomNodes();
95-
this.render(this.parentDomNode, nextSibling);
65+
if (this.reactComponent === undefined || this.reactComponent === null) {
66+
return;
67+
}
68+
if (this.root === undefined) {
69+
const nextSibling = this.findNextSiblingDomNode();
70+
this.render(this.parentDomNode, nextSibling);
71+
return;
72+
}
73+
this.computeAttributes();
74+
this.execute();
75+
const currentProps = this.getProps() ?? {};
76+
if (currentProps.parentWidget === undefined || currentProps.parentWidget === null) {
77+
currentProps.parentWidget = this;
78+
}
79+
const reactElement = React.createElement(this.reactComponent, currentProps);
80+
this.root.render(reactElement);
9681
}
9782

98-
removeChildDomNodes() {
99-
super.removeChildDomNodes();
83+
destroy() {
84+
// this only works after tiddlywiki 5.3.0
85+
super.destroy?.();
10086
this.root?.unmount?.();
10187
}
10288
}

0 commit comments

Comments
 (0)