Skip to content

Note editor addon#8011

Closed
krmanik wants to merge 29 commits intoankidroid:masterfrom
krmanik:note-editor-addon
Closed

Note editor addon#8011
krmanik wants to merge 29 commits intoankidroid:masterfrom
krmanik:note-editor-addon

Conversation

@krmanik
Copy link
Copy Markdown
Member

@krmanik krmanik commented Jan 11, 2021

Pull Request template

Purpose / Description

This continuation of previous PR #7958. As Anki desktop have python for creating addons. But in AnkiDroid Java and JavaScript can be used to create addons. This PR is implemented for running JavaScript addon.

Fixes

Fixes #7959

Approach

Most of enable and disable logic part and code of PR #7958 is used in this. But for note editor tool bar.

  1. Created Horizontal recycler view at bottom

  2. Added button when SharedPreferences have enable status of addons.

  3. Copy the index.js file code and pass to js-evaluator function.

  4. When a string selected in Note editor and Addon button pressed then selected text in FieldEditText passed to js-evaluator and the function defined in index.js file perform logic on that string then return it to caller.

How Has This Been Tested?

  1. Create addon with following file and folder structure and paste it in AnkiDroid / addons / ankidroid-js-addon-some-name
ankidroid-js-addon-some-name
↳ package
    ↳ package.json
      index.js
      Readme.md

The package.json file contains following for converting space separated text to list.

{
  "name": "ankidroid-js-addon-bullet",
  "version": "1.0.0",
  "description": "Convert list to bullet",
  "main": "index.js",
  "ankidroid_js_api": "0.0.1",
  "addon_type":"note_editor",
  "addon_icon": "="
}

Note: The package.json must contains name, ankidroid_js_api, addon_type, addon_icon. Here addon icon single letter.

  1. And index.js file contains following
function AnkiJSFunction(data) {
    var s = "";
    data = data.split(" ");
    for (i=0; i<data.length; i++) {
        s += "<li>" + data[i] + "</li>\n";
    }
    s = "<ul>" + s + "</ul>";

    return s;
}

Note: The name in index.js must be AnkiJSFunction it will be called by js-evaluator.

  1. Enable the addon from AnkiDroid->Addons->Note Editor Addons -> addon-name
  2. Now use it in Note Editor

Demo

Learning (optional, can help others)

https://github.com/evgenyneu/js-evaluator-for-android/
https://developer.android.com/guide/topics/ui/layout/recyclerview

Checklist

Please, go through these checks before submitting the PR.

  • [ x ] You have not changed whitespace unnecessarily (it makes diffs hard to read)
  • [ x ] You have a descriptive commit message with a short title (first line, max 50 chars).
  • [ x ] Your code follows the style of the project (e.g. never omit braces in if statements)
  • [ x ] You have commented your code, particularly in hard-to-understand areas
  • [ x ] You have performed a self-review of your own code
  • [ x ] UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)

@krmanik
Copy link
Copy Markdown
Member Author

krmanik commented Jan 11, 2021

I have tried multiple approach for text transformation. As it transform the text as per function deined in index.js but the previous selected not removed from edit text.
For example, the Field Edit Text have following text

AnkiDroid AnkiMobile AnkiDesktop

After selecting those text and then calling AnkiJSFunction from index.js function using js-evaluator, it transform the text return the following string to caller function

<ul><li>AnkiDroid</li>
<li>AnkiMobile</li>
<li>AnkiDesktop</li>
</ul>AnkiDroid AnkiMobile AnkiDesktop

The text AnkiDroid AnkiMobile AnkiDesktop is not cleared. I tried modifyCurrentSelection and mToolbar.onformat but didn't work.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 11, 2021

Codecov Report

Merging #8011 (89e293f) into master (393a959) will decrease coverage by 0.42%.
The diff coverage is 11.65%.

❗ Current head 89e293f differs from pull request most recent head e1a99c2. Consider uploading reports for the commit e1a99c2 to get more accurate results
Impacted file tree graph

@@             Coverage Diff              @@
##             master    #8011      +/-   ##
============================================
- Coverage     37.02%   36.59%   -0.43%     
+ Complexity     3847     3801      -46     
============================================
  Files           361      360       -1     
  Lines         36337    36384      +47     
  Branches       4804     4798       -6     
============================================
- Hits          13453    13316     -137     
- Misses        21352    21552     +200     
+ Partials       1532     1516      -16     
Impacted Files Coverage Δ Complexity Δ
...Droid/src/main/java/com/ichi2/anki/AddonModel.java 0.00% <0.00%> (ø) 0.00 <0.00> (?)
...id/src/main/java/com/ichi2/anki/AddonsAdapter.java 0.00% <0.00%> (ø) 0.00 <0.00> (?)
.../java/com/ichi2/anki/NavigationDrawerActivity.java 30.28% <0.00%> (-0.71%) 13.00 <0.00> (ø)
...id/src/main/java/com/ichi2/anki/AddonsBrowser.java 3.21% <3.21%> (ø) 2.00 <2.00> (?)
...Droid/src/main/java/com/ichi2/anki/NoteEditor.java 36.48% <18.91%> (-1.36%) 121.00 <5.00> (+4.00) ⬇️
...a/com/ichi2/anki/noteeditor/AddonToolsAdapter.java 20.00% <20.00%> (ø) 1.00 <1.00> (?)
...n/java/com/ichi2/anki/AbstractFlashcardViewer.java 38.87% <41.46%> (-0.72%) 168.00 <2.00> (-1.00)
...ava/com/ichi2/anki/noteeditor/AddonToolsModel.java 66.66% <66.66%> (ø) 1.00 <1.00> (?)
...c/main/java/com/ichi2/anki/noteeditor/Toolbar.java 50.71% <100.00%> (+1.40%) 14.00 <1.00> (ø)
...kiDroid/src/main/java/com/ichi2/utils/MapUtil.java 0.00% <0.00%> (-50.00%) 0.00% <0.00%> (-2.00%)
... and 98 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 393a959...e1a99c2. Read the comment docs.

@krmanik krmanik marked this pull request as draft January 12, 2021 08:35
@krmanik
Copy link
Copy Markdown
Member Author

krmanik commented Jan 12, 2021

Needs improvement

@krmanik
Copy link
Copy Markdown
Member Author

krmanik commented Mar 18, 2021

Update:
Show / hide addon list in toolbar using arrow button.

send deck name, model name, fields count, fields name and selected text in json format to js addon

receive json format data with changed text and data that needs to be inserted to fields with index
@krmanik
Copy link
Copy Markdown
Member Author

krmanik commented Mar 18, 2021

Update:

AnkiDroid  -->  JS Addon
send deck name, model name, fields count, fields name and selected text in json format to js addon
AnkiDroid  <--  JS Addon
receive json format data with changed text and data that needs to be inserted to fields with index

provide these to js addon in json format and receive changed data in json format

AnkiDroid  -->  JS Addon
 { 
   "noteType": "Basic",
   "deckName": "Default",
   "fieldsName": ["Front", "Back"],
   "fieldsCount": 2,
   "selectedText": "Some selected text..." 
}

data changed by js addon and pass to AnkiDroid

AnkiDroid  <--  JS Addon
 { 
  "changedText": "some changed text...",
  "addToFields": { 
                 "0": "some text to field one",
                 "2": "some text to field two"
                 "3": "some text to field three" 
                 },
  "changeOption": "replace"
}

To be implemented

change option - replace, append, clear, default - with selected text

Usage of above implementation in new AnkiDroid Cloze Overlapper Mini JS Addon

View index.js
function AnkiJSFunction(data) {

    var jsonData = JSON.parse(data);

    var selectedText = jsonData['selectedText'];

    var newJsonData = {};
    var fieldsData = {};
    if (jsonData['noteType'] == "ocloze-infi" && jsonData["fieldsCount"] == 26) {
        newJsonData["changedText"] = "";  // intentionally empty

        var list = selectedText.split("\n");

        var fullText = "";
        var len = list.length;
        var startField = 5;
        for (i = 0; i < len; i++) {
            var text = "";
            fullText += "<div>{{c21::" + list[i] + "}}</div>";
            for (j = 0; j < len; j++) {
                if (i == j) {
                    if (j > 0) {
                        text += "<div>" + list[j - 1] + "</div>";
                    }
                    text += "<div>{{c" + (j + 1) + "::" + list[j] + "}}</div>";
                } else {
                    text += "<div>...</div>";
                }
            }
            fieldsData[startField] = text;
            startField++;
        }
        fieldsData[25] = fullText;

        newJsonData["addToFields"] = fieldsData;
    }

    return JSON.stringify(newJsonData);
}

Cloze generated by js addon

option to change selected text with following option :  replace, append, clear, default
@krmanik
Copy link
Copy Markdown
Member Author

krmanik commented Mar 18, 2021

Implemented: change selected text according to option : replace, clear, append and default

Click to view index.js
AnkiDroid  <-- JS Addon

function AnkiJSFunction(data) {

    var jsonData = JSON.parse(data);

    var selectedText = jsonData['selectedText'];

    var newJsonData = {};

    newJsonData["changedText"] = "<sub>"+ selectedText +"</sub>";

    // changeType options - replace, append, clear, default
    newJsonData["changeType"] = "replace";

    return JSON.stringify(newJsonData);
}

@krmanik krmanik marked this pull request as ready for review March 19, 2021 09:27
@krmanik krmanik marked this pull request as draft March 19, 2021 09:35
@krmanik
Copy link
Copy Markdown
Member Author

krmanik commented Mar 19, 2021

This continuation of previous PR #7958. So, I will mark it ready for review after merge of previous one. Some files are same in both PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jun 1, 2021

Hello 👋, this PR has been opened for more than 2 months with no activity on it. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically

@mikehardy
Copy link
Copy Markdown
Member

Ugh - not stale just so maxed out stabilizing all the GSoC work on the release-2.15 branch

@mikehardy mikehardy added the Keep Open avoids the stale bot label Jun 1, 2021
@krmanik
Copy link
Copy Markdown
Member Author

krmanik commented Jun 6, 2021

I will re-implement this js addons feature with more number of PR. I will divide this PR in small part so reviewing will take less time.

@krmanik krmanik closed this Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Keep Open avoids the stale bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] JavaScript Addons support for AnkiDroid

3 participants