Skip to content

Commit

Permalink
fix: usage of draft title
Browse files Browse the repository at this point in the history
fixes #29
  • Loading branch information
linonetwo committed Mar 30, 2024
1 parent e82bc37 commit 6d8ba9a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"requestidlecallback-polyfill": "^1.0.2",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"tw-react": "^0.6.2",
"tw-react": "^0.6.3",
"tw5-typed": "^0.5.7",
"typescript": "^5.4.3"
},
Expand Down
14 changes: 10 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/tw-whiteboard/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function App(props: IAppProps & IDefaultWidgetProps): JSX.Element {
parentWidget,
isDarkMode,
locale,
isDraft,
onReady,
} = props;

Expand Down Expand Up @@ -141,9 +142,10 @@ export function App(props: IAppProps & IDefaultWidgetProps): JSX.Element {

// emergency save on close or switch to other editor (by changing the type field) or readonly
// this only work as willUnMount
// useless since we have edit template
useEffect(() => {
return () => {
if (readonly) return;
if (readonly ?? isDraft) return;
void (async () => {
if (!editor) return;
onSave(await serializeTldrawJson(editor.store));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
title: $:/plugins/linonetwo/tw-whiteboard/tiddlywiki-ui/EditTemplate/body/edit-whiteboard

\define lingo-base() $:/language/EditTemplate/Body/
\procedure lingo-base() $:/language/EditTemplate/Body/

<div class="tw-whiteboard-edit-body-container">
<!-- title is `"Draft of 'AAA'"` from <<currentTiddler>> when in draft mode. -->
<$set name="tiddler" value={{!!title}} emptyValue=<<currentTiddler>>>
<$edit-whiteboard tiddler=<<tiddler>> draftTitle={{!!draft.title}} height="500px" />
</$set>
<!-- title is `"Draft of 'AAA'"` from <<currentTiddler>> when in draft mode. -->
<$edit-whiteboard tiddler=<<currentTiddler>> draftTitle={{!!draft.title}} height="500px" />
</div>
15 changes: 11 additions & 4 deletions src/tw-whiteboard/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/* eslint-disable @typescript-eslint/triple-slash-reference */
/// <reference path="widget-type.d.ts" />
/* eslint-disable @typescript-eslint/no-explicit-any */
declare module '$:/plugins/linonetwo/tw-react/widget.js' {
import type { FullReactDomType, IReactWidget, ITWReactProps, ITWReactPropsDefault } from 'tw-react';
import type { widget as Widget } from '$:/core/modules/widgets/widget.js';
import type { IReactWidget, ITWReactProps, ITWReactPropsDefault } from 'tw-react/widget-type';
import type { Widget } from 'tiddlywiki';
import type * as ReactType from 'react';
// import type * as ReactDomType from 'react-dom';
import type * as ReactDomClientType from 'react-dom/client';

export abstract class widget<
IProps extends ITWReactProps = ITWReactPropsDefault,
> extends Widget implements IReactWidget<IProps> {
root?: ReturnType<FullReactDomType['createRoot']> | undefined;
root?: ReturnType<typeof ReactDomClientType['createRoot']> | undefined;
containerElement?: HTMLDivElement | undefined;
destroy(): void;
getProps?: () => IProps;
/**
* User of tw-react need to assign his react component to this property.
Expand All @@ -21,5 +27,6 @@ declare module '$:/plugins/linonetwo/tw-react/widget.js' {
}

declare module '$:/plugins/linonetwo/tw-react/index.js' {
export * from 'tw-react';
export * from 'tw-react/dist/lib/hooks';
export * from 'tw-react/dist/lib/widget-type';
}
17 changes: 15 additions & 2 deletions src/tw-whiteboard/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TldrawWhiteBoardWidget extends Widget<IAppProps> {
readonly: this.getAttribute('readonly') === 'yes' || this.getAttribute('readonly') === 'true',
zoomToFit: this.getAttribute('zoomToFit') !== 'no' && this.getAttribute('zoomToFit') === 'false',
zoom: this.getAttribute('zoom'),
isDraft: this.editTitle === undefined ? false : Boolean(this.getAttribute('draftTitle')),
isDraft: this.isDraft,
locale: $tw.wiki.getTiddlerText('$:/language') === '$:/languages/zh-Hans' ? 'zh-cn' : 'en',
isDarkMode: $tw.wiki.getTiddler($tw.wiki.getTiddlerText('$:/palette') ?? '')?.fields?.['color-scheme'] === 'dark',
onReady: this.onReady,
Expand All @@ -36,6 +36,15 @@ class TldrawWhiteBoardWidget extends Widget<IAppProps> {
this.ready = true;
};

private get isDraft() {
return this.editTitle === undefined ? false : Boolean(this.getAttribute('draftTitle'));
}

destroy() {
super.destroy?.();
this.ready = false;
}

public refresh(changedTiddlers: IChangedTiddlers): boolean {
if (!this.ready) return false;
if (changedTiddlers['$:/state/Whiteboard/PageLayout/focusedTiddler'] || changedTiddlers['$:/palette'] || changedTiddlers['$:/language']) {
Expand Down Expand Up @@ -74,14 +83,18 @@ class TldrawWhiteBoardWidget extends Widget<IAppProps> {
if (this.editTitle === '' || this.editTitle === undefined) {
return;
}
// prevent save after destroy. On react unmount, emergency save in its willUnmount will try to call onSave. But when in story view and it is draft, this will cause save draft while tw is trying to delete draft. Cause draft not delete after end editing.
if (this.isDraft && !this.ready) return;
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
const previousTiddler = $tw.wiki.getTiddler(this.editTitle);
// prevent useless call to addTiddler
if (previousTiddler?.fields.text !== newText) {
// use setText for DraftTiddler, otherwise if use addTiddler we will make it a real tiddler immediately.
$tw.wiki.setText(this.editTitle, 'text', undefined, newText);
// set tiddler type
$tw.wiki.setText(this.editTitle, 'type', undefined, 'application/vnd.tldraw+json');
if (previousTiddler?.fields.type !== 'application/vnd.tldraw+json') {
$tw.wiki.setText(this.editTitle, 'type', undefined, 'application/vnd.tldraw+json');
}
}
this.unlock();
};
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"declaration": false,
"jsx": "react-jsx",
"baseUrl": "./",
"typeRoots" : ["node_modules/@types", "node_modules/tw5-typed", "node_modules/tw-react/dist/lib"],
"typeRoots" : ["./node_modules/@types", "./node_modules/tw5-typed", "./node_modules/tw-react/dist/lib"],
"lib": ["DOM", "ESNext"]
},
"include": ["src"],
Expand Down

0 comments on commit 6d8ba9a

Please sign in to comment.