TW5 - replacing all my old inline JS

閲覧: 616 回
最初の未読メッセージにスキップ

tejjyid

未読、
2017/02/28 6:04:572017/02/28
To: TiddlyWiki
I've managed to update many of my simpler macros, but now I'm moving on to the more complicated ones.

There are two scenarios that I used to have:

1.) a list of words stored in one tiddler that I would highlight in a range of texts stored in other tiddlers. That list of words would be converted into a regex via some inline js, and I could then use that regex in other macros, because once created the regex variable was "visible" everywhere. From my point of view, this was an easy option because I could update the list of words to be annotated easily.

2.) I had a library of functions stored in one tiddler that I could use in varying combinations in different macros. 

I can't work out to do either thing in the TW5 framework. I don't mind doing it a better way, if the better way doesn't involve complex code. Or alternatively, how do I make the same arrangement work?

Something else - I used to be able to get tiddlers into my macros with code like "store.getTiddlerText" - does that functionality still exist? Can someone point me to where it's documented?

If anyone feels they need to look at old code to answer the questions: the attached has everything. But probably some ideas on how to do it & pointers to useful documentation would be enough.

Thanks 
andrewsimon(1).html

Jeremy Ruston

未読、
2017/02/28 7:21:082017/02/28
To: tiddl...@googlegroups.com
Hi tejjyid

I've managed to update many of my simpler macros, but now I'm moving on to the more complicated ones.

There are two scenarios that I used to have:

1.) a list of words stored in one tiddler that I would highlight in a range of texts stored in other tiddlers. That list of words would be converted into a regex via some inline js, and I could then use that regex in other macros, because once created the regex variable was "visible" everywhere. From my point of view, this was an easy option because I could update the list of words to be annotated easily.

The simplest type of JavaScript module to write for TiddlyWiki5 is a JS macro. The semantics are extremely simple: the macro defines the names of its parameters, and exports a handler function that is called when the macro is invoked. Here’s a simple example from the core:


There are quite a few restrictions and limitations with macros, though.

Firstly, a macro is not supposed to have any side effects (it cannot create or modify tiddlers, for example), and is supposed to return the same result every time it is called with a given set of parameters.

Secondly, they don’t participate in the refresh mechanism (which is one of the things that makes them simple to write). This means that you can’t easily arrange for them to automatically refresh if underlying data should change.

Nonetheless, in many situations, writing a macro is adequate. There is some documentation here:


In your case, you could write a macro <<extract-regexp filter>> that would retrieve the tiddlers matching a filter, and retrieve the highlights from inside them, and then return the resulting regexp.

Then, you could use an action-setfield widget within a button to allow the user to manually trigger the assignment of the regular expression to a tiddler.

2.) I had a library of functions stored in one tiddler that I could use in varying combinations in different macros. 

I can't work out to do either thing in the TW5 framework. I don't mind doing it a better way, if the better way doesn't involve complex code. Or alternatively, how do I make the same arrangement work?

TiddlyWiki 5 provides a Common/JS compatible module loader that makes it easy to require() other modules and access their exports. Here’s an example from one of the core plugins:


Something else - I used to be able to get tiddlers into my macros with code like "store.getTiddlerText" - does that functionality still exist? Can someone point me to where it's documented?

Within a macro, you can obtain the text of a tiddler with:

this.wiki.getTiddlerText("HelloThere")

TiddlyWiki’s internal APIs are only documented within the source. Most of the TiddlyWiki Classic store.*() functions will be found in:


Generally, the trick with TiddlyWiki 5 development is to find an existing plugin or module that you can use as a starting point.

Best wishes

Jeremy

If anyone feels they need to look at old code to answer the questions: the attached has everything. But probably some ideas on how to do it & pointers to useful documentation would be enough.

Thanks 

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/2d4f51a2-bc48-489f-ae41-90c018d01429%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<andrewsimon(1).html>

BJ

未読、
2017/02/28 11:36:362017/02/28
To: TiddlyWiki

I have had a quick hack to indicate what I think may be the best approach - see attached,

all the best
BJ
testgrammer.html

tejjyid

未読、
2017/03/01 4:01:422017/03/01
To: TiddlyWiki
That is exceptionally generous of you! I think I can puzzle out a lot about the internals from your's and Jeremy's posts. Appeciate it.

It may be a bridge too far (and maybe I can work it out with what you've already given me), but if you could stand to look at POSTagger.js: mainly because it uses a very large variable LEXICON (which is in Lexicon.js_) and I'm wondering what the best way to handle that volume of data is. It used to break IE8, but it's slow-ish to load anywhere.

Thanks regardless; you've been very helpful. WHen I finish this lot I'll be free to start thinking about the content and not the machinery.

Andrew

tejjyid

未読、
2017/03/01 4:28:312017/03/01
To: TiddlyWiki
Thanks Jeremy - I don't think the lack of refresh will be an issue (as I understand the limitation), but I can  cross the bidge if I need to later. I suspect you've given me enough to go on, and I think I can puzzle out the "require" and "exports" mechanism, but I do have a couple of questions if you have the time:

My google-driven understanding of require/exports is that it's node, not javascript. I know TW5 is built around node, but I'm not running it. So, is the TW5 core emulating something in mode? Is there a good place to read up on this? Also, I had a look at 

var Widget = require("$:/core/modules/widgets/widget.js").widget;
, which I assume is what you wanted me to look at.

I'm sure a bunch of trial-and-error will get me home, but:
1.) I don't think I'll be putting my code into "$:/core/modules" anytime soon. Or is that where they have to go for this to work?
2.) is that statement a specific instance of "var Anyname = require("anywhere/anycode.anysuffix").anymethod? It's not clear to me which of those elements are by documentary convenience & which are by necessity.

I would love to find stuff to copy/modify, but it's easier said than done. When I got into this originally, I couldn't find anyone else interested in analysing/annotating text.  I had the time to look at a ton of TWC stuff then; now, it's taken me a year to get around to getting out of Tiddlyspace & upgrading to TW5.

Anyway I very much appreciate your work & help. I actually submitted my MA research paper as a TWC tiddlywiki a few years ago - my supervisor was so impressed by the technology she forgot to check the content, which was fine by me :-) The examiners likewise. (That had things in it that were more "standard"; bibliography, for example, so copy/modify was easier) Probably I should offer to share it with you.

Cheers, Andrew


On Tuesday, 28 February 2017 23:21:08 UTC+11, Jeremy Ruston wrote:
Hi tejjyid

I've managed to update many of my simpler macros, but now I'm moving on to the more complicated ones.

There are two scenarios that I used to have:

1.) a list of words stored in one tiddler that I would highlight in a range of texts stored in other tiddlers. That list of words would be converted into a regex via some inline js, and I could then use that regex in other macros, because once created the regex variable was "visible" everywhere. Frbut I can crossom my point of view, this was an easy option because I could update the list of words to be annotated easily.
メッセージは削除されました

tejjyid

未読、
2017/03/02 4:35:582017/03/02
To: TiddlyWiki
OK, I think I have it 90% sussed. I'll try writing it up when I've done some stuff with it. Thanks again.

tejjyid

未読、
2017/03/03 6:10:512017/03/03
To: TiddlyWiki
I have a couple more questions if you have time:

1.) I notice your macro isn't wraped in a (function(){...} structure and was wondering why? I also noticed that there was no $:/tags/Macro tag, but I've noticed that doesn't seem fundamental on my own macros.

2.) I slavishly copied your structure, changing only the names (I thought) but for reasons obscure to me my "shared.js" tiddler, which is supposed to be identical to your "commondefs" tiddler, isn't found by the require statement. I've attached them in case you feel like tracking down my error. So far as I can tell the tagging, tiddler type & fields are the same, but it's late here....

Thanks in anticipation (but it's unreasonable, sure, I understand that)

 

On Wednesday, 1 March 2017 03:36:36 UTC+11, BJ wrote:
testgrammer (1).html

BJ

未読、
2017/03/03 7:43:112017/03/03
To: TiddlyWiki
As you can see the wrapper isn't need - actual when the module is compiled its source gets copied inside a wrapper ...

$:/tags/Macro is used for wikitext macros, js marcos have an extra field 'modules-type' set to 'macro'.

all js tiddlers need a 'modules-type' otherwise they will not get compiled. In the case of your 'shared.js'  it needs 'modules-type' but leave the value blank.

all the best

BJ
全員に返信
投稿者に返信
転送
新着メール 0 件