1/**
2 * Editor button
3 */
4if (typeof window.toolbar !== 'undefined') {
5    window.toolbar[window.toolbar.length] = {
6        type: "plugindoi",
7        title: LANG.plugins.doi.toolbarButton,
8        icon: "../../plugins/doi/img/library.png",
9   };
10}
11
12function tb_plugindoi(btn, props, edid) {
13    PluginDoi.edid = edid;
14    PluginDoi.buildSyntax();
15}
16
17const PluginDoi = {
18    edid: null,
19
20    /**
21     * Ask for ID and check format to determine DOI or ISBN syntax
22     */
23    buildSyntax: function () {
24
25        const ident = prompt(LANG.plugins.doi.prompt);
26        if (!ident) return;
27
28        const isbnRegex = new RegExp('^((?:[0-9X][- ]?){10}|(?:[0-9X][- ]?){13})$', 'i');
29        if (ident.match(isbnRegex)) {
30            PluginDoi.insert('isbn', ident);
31            return;
32        }
33
34        const doiRegex = new RegExp('(10[.][0-9]{4,}[^\\s"\\/<>]*\\/[^\\s"<>]+)');
35        if (ident.match(doiRegex)) {
36            PluginDoi.insert('doi', ident);
37            return;
38        }
39
40        alert(LANG.plugins.doi.noMatch);
41    },
42
43    /**
44     * Insert syntax
45     *
46     * @param {string} key
47     * @param {string} ident
48     */
49    insert: function (key, ident) {
50        const syntax = '[[' + key + '>' + ident + ']]';
51        insertAtCarret(PluginDoi.edid, syntax);
52    }
53};
54