Skip to content

Commit

Permalink
adding some examples to AttributesRO reference
Browse files Browse the repository at this point in the history
  • Loading branch information
EdoFro committed Nov 30, 2023
1 parent 2be85ba commit 5707b20
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build
/build
/.idea
/freeplane_docs.iml
10 changes: 9 additions & 1 deletion src/docs/scripting/Reference.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Scripting Reference (work in progress)

To test the example codes you can use the Freeplane's script editor:
- select the test node
- open the script editor ( **Menu->Tools->Edit script…** )
- create a new script ( **Actions->New script** )
- paste the example code in the editor
- test the code ( **Actions->Run** )
- if you want, you can select other nodes and repeat the test

```groovy
{{#include reference.groovy}}
```

<footer id="open-on-gh">
Want to contribute? <a href="https://github.com/freeplane/docs/edit/main/src/docs/scripting/reference.groovy">Edit the groovy file on github.</a>
</footer>
</footer>
114 changes: 103 additions & 11 deletions src/docs/scripting/reference.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,136 @@
AttributesRO
*/

// Returns "true", if the selected node contains an attribute key that is equal to "aaa". Returns "false", otherwise.
/*
For the following code examples you can paste the following branch in Freeplane.
Select and copy the following lines and paste as a branch from the selected node in your mind map.
Sample Branch 01
item A: Attributes
item A1
+ myAttribute my text value
+ aaa v_aaa
item A2
+ otherAttribute aaa
item A3
item A4
+ aaa first
+ bbb second
+ aaa third
+ aaa fourth
item B
item C
*/

// Returns "true", if the selected node contains an attribute key that is equal to "aaa". Returns "false", otherwise.
/*
using "Sample Branch 01":
selecting
node "item A1" returns true
node "item A2" returns false
node "item A3" returns false
node "item A4" returns true
*/
node.attributes.containsKey("aaa")

// Returns the index of the first attribute with the name "aaa" if one exists or -1 otherwise.

/*
using "Sample Branch 01":
selecting
node "item A1" returns 1 (it's the second attribute)
node "item A2" returns -1
node "item A3" returns -1
node "item A4" returns 0 (the first one is in the first attribute's position)
*/
node.attributes.findFirst("aaa")

// Returns the attribute value at the index 1.

/*
using "Sample Branch 01":
selecting
node "item A1" returns 'v_aaa'
node "item A2" returns exception: "org.freeplane.plugin.script.ExecuteScriptException: Array index out of range: 1, Array index out of range: 1"
node "item A3" returns exception: "org.freeplane.plugin.script.ExecuteScriptException: Array index out of range: 1, Array index out of range: 1"
node "item A4" returns 'second'
*/
node.attributes.get(1)

// Returns the values of all the attributes that has the key equal to "aaa".

/*
using "Sample Branch 01":
selecting
node "item A1" returns [v_aaa]
node "item A2" returns []
node "item A3" returns []
node "item A4" returns [first, third, fourth]
*/
node.attributes.getAll("aaa")

// Returns the first value of an attribute with the name "aaa" or null otherwise.

/*
using "Sample Branch 01":
selecting
node "item A1" returns v_aaa
node "item A2" returns null
node "item A3" returns null
node "item A4" returns first
*/
node.attributes.getFirst("aaa")

// Returns the attribute key at the index 1.

/*
using "Sample Branch 01":
selecting
node "item A1" returns aaa
node "item A2" returns exception: "org.freeplane.plugin.script.ExecuteScriptException: Array index out of range: 1, Array index out of range: 1"
node "item A3" returns exception: "org.freeplane.plugin.script.ExecuteScriptException: Array index out of range: 1, Array index out of range: 1"
node "item A4" returns bbb
*/
node.attributes.getKey(1)

// Returns all attributes as a map.

/*
using "Sample Branch 01":
selecting
node "item A1" returns {myAttribute=my text value, aaa=v_aaa}
node "item A2" returns {otherAttribute=aaa}
node "item A3" returns {}
node "item A4" returns {aaa=fourth, bbb=second} // (note that it takes for aaa the value of the last attribute)
*/
node.attributes.map

// Returns all attribute names in the proper sequence.

/*
using "Sample Branch 01":
selecting
node "item A1" returns [myAttribute, aaa]
node "item A2" returns [otherAttribute]
node "item A3" returns []
node "item A4" returns [aaa, bbb, aaa, aaa]
*/
node.attributes.names

// Returns "true", if the selected node has no attributes. Returns "false", otherwise.

node.attributes.empty
/*
using "Sample Branch 01":
selecting
node "item A1" returns false
node "item A2" returns false
node "item A3" returns true
node "item A4" returns false
*/
node.attributes.empty

// Returns the number of attributes.

/*
using "Sample Branch 01":
selecting
node "item A1" returns 2
node "item A2" returns 1
node "item A3" returns 0
node "item A4" returns 4
*/
node.attributes.size()

/*
Expand Down

0 comments on commit 5707b20

Please sign in to comment.