grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Adding a working Import Panel Button without the presets

Open ivanezeigbo opened this issue 8 years ago • 2 comments

I want to add the Import button at the panel for importing templates, but I don't want to use the presets. I have already added the following code:

... { id: 'import', className: 'fa fa-download', attributes: {title: 'Import Your Template'}, command: function(editor, sender){ ???? } } ...

I don't know what should be in the command. Please, could you tell me the function to use for command?

ivanezeigbo avatar Jun 17 '18 17:06 ivanezeigbo

Oh, I just figured it out. So, for those who might be curious about how to solve this, this is how I went about it (by the way, this is thanks to the answer in Issue #324 :

var pfx = editor.getConfig().stylePrefix;
							var modal = editor.Modal;
							var cmdm = editor.Commands;
							var codeViewer = editor.CodeManager.getViewer('CodeMirror').clone();
							var pnm = editor.Panels;
							var container = document.createElement('div');
							var btnEdit = document.createElement('button');

							codeViewer.set({
							    codeName: 'htmlmixed',
							    readOnly: 0,
							    theme: 'hopscotch',
							    autoBeautify: true,
							    autoCloseTags: true,
							    autoCloseBrackets: true,
							    lineWrapping: true,
							    styleActiveLine: true,
							    smartIndent: true,
							    indentWithTabs: true
							});

							btnEdit.innerHTML = 'Import';
							btnEdit.className = pfx + 'btn-prim ' + pfx + 'btn-import';
							btnEdit.onclick = function() {
							    var code = codeViewer.editor.getValue();
							    editor.DomComponents.getWrapper().set('content', '');
							    editor.setComponents(code.trim());
							    modal.close();
							};

							cmdm.add('html-import', {
							    run: function(editor, sender) {
							        sender && sender.set('active', 0);
							        var viewer = codeViewer.editor;
							        modal.setTitle('Edit code');
							        if (!viewer) {
							            var txtarea = document.createElement('textarea');
							            container.appendChild(txtarea);
							            container.appendChild(btnEdit);
							            codeViewer.init(txtarea);
							            viewer = codeViewer.editor;
							        }
							        modal.setContent('');
							        modal.setContent(container);
							        codeViewer.setContent('');
							        modal.open();
							        viewer.refresh();
							    }
							});

							pnm.addButton('options',
							    [
							        {
							            id: 'import',
							            className: 'fa fa-download',
							            command: 'html-import',
							            attributes: {
							                title: 'Import Your Template'
							            }
							        }
							    ]
							);

ivanezeigbo avatar Jun 17 '18 19:06 ivanezeigbo

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 17 '19 12:09 lock[bot]