TiddlyWiki / TiddlyWiki5

A self-contained JavaScript wiki for the browser, Node.js, AWS Lambda etc.
https://tiddlywiki.com/
Other
8.05k stars 1.19k forks source link

[BUG] when loading module-type: syncadaptor, we should use last founded one #7553

Closed linonetwo closed 8 months ago

linonetwo commented 1 year ago

Describe the bug

We have tiddlyweb with priority 10, which should work when we access wiki from the browser. And I have a tidgi-ipc-adaptor plugin with priority 11, that should work when we access the wiki in the Electron.

In electron env this works fine, but when in the web:

Based on "plugin-priority": 11 in plugin.info, a plugin with a larget priority should overwrite all smaller ones. Now tiddlyweb's wikitext will be overwritten by tidgi-ipc-adaptor's wikitext. Even tidgi-ipc-adaptor currently is disabled by not exposing adaptorClass.

What if we change tidgi-ipc-adaptor plugin with priority 9 to avoid this? Then in the web it works fine, but in Electron env my wikitext UI will be overwritten by tiddlyweb's UI.

Expected behavior

Wikitext should be enabled/disabled with the syncadaptor module. Otherwise

To Reproduce

Well...A bit difficult to construct.

Screenshots

While the working syncadaptor is tidgi-ipc-syncadaptor, the showing wikitext ui is from tiddlyweb-plugin

图片

TiddlyWiki Configuration

Desktop (please complete the following information):

Additional context

I'm adding a ipc syncadaptor that works in electron faster.

linonetwo commented 1 year ago

Oh, no, the major problem is: even a syncadaptor plugin's JS is not loaded, its wikitext is still shadowing previous syncadaptor plugin's wikitext UI!

We need a way to inform high priority syncadaptor's UI to not overwrite low-priority-but-working plugin's ui.

Jermolene commented 1 year ago

Thank you @linonetwo. It sounds like you're trying to make a wiki that has two different sync adaptor plugins loaded at the same time, and have the system dynamically choose which one to use according to the environment in which it is loaded.

However, at the moment, dynamically switchable sync adaptors are not supported by the system. The tiddlywebadaptor is active if it is loaded in a wiki, and there is no way to deactivate it without removing the plugin.

I'm not sure how Tidgi works, but might it be possible to splice in the correct syncadaptor plugin at the time the HTML file is being loaded into Electron's webview?

linonetwo commented 1 year ago

have the system dynamically choose which one to use according to the environment in which it is loaded

Yes, I'm currently registering my syncadaptor depending on environment


if ($tw.browser && typeof window !== 'undefined') {
  const isInTidGi = typeof document !== 'undefined' && document?.location?.protocol?.startsWith('tidgi');
  const servicesExposed = Boolean(window.service?.wiki);
  const hasWorkspaceIDinMeta = Boolean((window.meta as WindowMeta[WindowNames.view] | undefined)?.workspaceID);
  if (isInTidGi && servicesExposed && hasWorkspaceIDinMeta) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
    exports.adaptorClass = TidGiIPCSyncAdaptor;
  }
}

https://github.com/tiddly-gittly/TidGi-Desktop/blob/6fe1b958811a2b0fe4f08f92936340b269cb1e25/src/services/wiki/plugin/ipcSyncAdaptor/ipc-syncadaptor.ts#L354-L362

Which is learnt from tiddlyweb/tiddlywebadaptor.js

https://github.com/Jermolene/TiddlyWiki5/blob/13a895bd2daddabdfaadb04e20dbc7563c9c39be/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js#L375-L377

dynamically switchable sync adaptors

So I don't think I need to dynamically switch them (by user)? Because this happened automatically. And these JS parts just work together well.

The conflict happens in the wikitext part.

splice in the correct syncadaptor plugin at the time the HTML file is being loaded into Electron's webview

Do you mean using $tw.wiki.addTiddler? I tried, and filesystem plugin will write a JSON plugin into the /tiddlers folder... Only by preloading these plugins before booting, by doing:

wikiInstance.boot.extraPlugins = [
  'plugins/linonetwo/tidgi-ipc-syncadaptor',
  'plugins/linonetwo/tidgi-ipc-syncadaptor-ui',
  enableHTTPAPI ? 'plugins/tiddlywiki/tiddlyweb' : undefined,
]

https://github.com/tiddly-gittly/TidGi-Desktop/blob/6fe1b958811a2b0fe4f08f92936340b269cb1e25/src/services/wiki/wikiWorker/startNodeJSWiki.ts#L61-L72

can prevent tw write a JSON to /tiddlers folder.

linonetwo commented 1 year ago

dynamically switchable sync adaptors

So this might need an overhaul, because I hope in the future, nodejs wiki can sync to fs + sync to a tiddlyweb server + communicate between client/server, or even sync to solid server at the same time! Just like how Notion/Office365 feels.

Which will even requires 2 or more syncadaptor runs at the same time, acts like a multiplexer.

Jermolene commented 1 year ago

Yes, I'm currently registering my syncadaptor depending on environment

Great that looks OK.

The conflict happens in the wikitext part.

Is the problem that you're seeing the wikitext shadow tiddlers from the tiddlywebadaptor even if it is disabled?

splice in the correct syncadaptor plugin at the time the HTML file is being loaded into Electron's webview

I meant modifying the HTML file to add the plugins into the JSON store area.

Do you mean using $tw.wiki.addTiddler? I tried, and filesystem plugin will write a JSON plugin into the /tiddlers folder... Only by preloading these plugins before booting, by doing:

Great, if the preloading technique works that's much easier.

linonetwo commented 1 year ago

wikitext shadow tiddlers from the tiddlywebadaptor even if it is disabled

Yes, wikitext can't be disabled using JS, so they are shadowing even corresponding JS is disabled...

Wait, maybe I can access $tw.wiki.xxx to modify the shadow tiddlers, to disable my wikitext if my JS is disabled. This is a hacking way, but maybe is the only way, and we may have to do this in the tiddlyweb plugin too, or make this a core method, to become a standard way.