File size: 2,894 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Model from '@ckeditor/ckeditor5-ui/src/model';
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
import { addListToDropdown, createDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';

export default class TokenPlugin extends Plugin {
    init() {
        const editor = this.editor;
        const tokens = typeof editor.config._config.dynamicToken != undefined ? editor.config._config.dynamicToken : [] ;
        const tokens_label = typeof editor.config._config.dynamicTokenLabel != undefined ? editor.config._config.dynamicTokenLabel : "Insert Token" ;
        editor.ui.componentFactory.add('TokenPlugin', (locale) => {
            const dropdownView = createDropdown(locale);
            dropdownView.buttonView.set({
                withText: true,
                label: tokens_label,
                tooltip: true,
            });

            const items = new Collection();
            tokens.map((item) => {
                const itemId = item.id;
                let tokenName = item.name;
                let tokenNameArr = tokenName.split(':');
                if (tokenNameArr[0] != undefined && tokenNameArr[0] === 'a')
                {
                    tokenName =  tokenNameArr[1];
                }

                if (itemId.match(/dwc=/i)){
                    const tn = itemId.substr(5, itemId.length - 6);
                    tokenName = tokenName + ' (' + tn + ')';
                } else if (itemId.match(/contactfield=company/i) && !tokenName.match(/company/i)){
                    tokenName = 'Company ' + tokenName;
                }

                items.add({
                    type: 'button',
                    model: new Model({
                        withText: true,
                        label: tokenName,
                        id: itemId,
                    }),
                });
            });

            addListToDropdown(dropdownView, items);

            dropdownView.on('execute', (eventInfo) => {
                const {id, label} = eventInfo.source;
                editor.model.change(writer => {
                    let content = "<span class='atwho-inserted' data-fr-verified='true'>"+id+"</span>";
                    if (id.match(/assetlink=/i)) {
                        content = '<a title="Asset Link" href="' + id + '">' + label + '</a>';
                    } else if (id.match(/pagelink=/i)) {
                        content = '<a title="Page Link" href="' + id + '">' + label + '</a>';
                    }

                    const viewFragment = editor.data.processor.toView( content );
                    const modelFragment = editor.data.toModel( viewFragment );
                    editor.model.insertContent( modelFragment );
                });
            });

            return dropdownView;
        });
    }
};