Skip to content

tiddly-gittly/tw-react

Repository files navigation

TiddlyWiki React

What’s your opinion about using ReactJS in the plugin? Sometimes it is inevitable to use ReactJS in the widget, for example, JSONSchemaForm.

See Website for documentation.

Usage

Add this to a type.d.ts file to your src/ folder:

declare module '$:/plugins/linonetwo/tw-react/widget.js' {
  import { IReactWidgetConstructor } from 'tw-react';
  const widget: IReactWidgetConstructor;
}

or in tsconfig.json add:

    "typeRoots" : ["node_modules/@types", "node_modules/tw5-typed", "node_modules/tw-react/dist/lib"],

Products

  1. WhiteBoard plugin
  2. Demo of a new WYSIWYG editor: slate-write (unstable alpha stage)
  3. FlowTiwi - a Multi-column draggable resizable sidebar (beta release)
  4. Smart Form plugin - beta release - Display different form based on the tag you add

When installing these plugins from TiddlyWiki-CPL: TiddlyWiki world of Google App Store! , this React plugin will be automatically installed as a dependency. So you probably don't need to install this manyally.

(You can also drag __plugins_linonetwo_tw-react.json from Release · tiddly-gittly/tw-react · GitHub. But Don't drag them from above demo sites. Because the ReactJS plugin from those demosite is of dev mode, so size is much larger than the one in the CPL and Github release!)

Trouble shotting

React is undefined

You may need import React from 'react'; on the top of jsx file, even you are in react 17+ and not using this variable...

My css is not loading

See things in your dist/plugins/xxx/yyy, is there a zzz.css file?

Then you will need to create a zzz.css.meta file in your src folder:

title: $:/plugins/linonetwo/flowtiwi-sidebar/flowtiwi-sidebar.css
tags: $:/tags/Stylesheet
type: text/css

ReferenceError: window is not defined

You may have this error when booting nodejs wiki:

Error executing boot module $:/plugins/linonetwo/tw-whiteboard/widget.js: {}

$:/plugins/linonetwo/tw-whiteboard/widget.js:29527
var AY = window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)").matches : false;
         ^

ReferenceError: window is not defined
    at $:/plugins/linonetwo/tw-whiteboard/widget.js:29527:10
    at Script.runInContext (node:vm:139:12)
    at Script.runInNewContext (node:vm:144:17)

This is because in nodejs context, there are no window object.

You need to only execute your script in browser, like the example in slate-write:

/* eslint-disable @typescript-eslint/no-unsafe-assignment */
(function slateWriteWidgetIIFE() {
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
  if (!$tw.browser) {
    return;
  }
  // separate the widget from the exports here, so we can skip the require of react code if `!$tw.browser`. Those ts code will error if loaded in the nodejs side.
  const components = require('$:/plugins/linonetwo/slate-write/components/index.js');
  const { widget } = components;
  /* eslint-disable @typescript-eslint/no-unsafe-member-access */
  exports.slateWrite = widget;
  // fix `Undefined widget 'edit-slateWrite'`
  exports['edit-slateWrite'] = widget;
})();

While export the widget from another file:

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
exports.widget = SlateWriteWidget;