View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.ckeditor;
35  
36  
37  import static info.magnolia.ui.vaadin.gwt.client.ckeditor.VMagnoliaCKEditorTextEvents.*;
38  
39  import java.util.Arrays;
40  import java.util.List;
41  
42  import org.vaadin.alump.ckeditor.client.CKEditor;
43  import org.vaadin.alump.ckeditor.client.VCKEditorTextField;
44  
45  import com.google.gwt.user.client.Timer;
46  import com.vaadin.client.ApplicationConnection;
47  import com.vaadin.client.UIDL;
48  import com.vaadin.client.ValueMap;
49  import com.vaadin.client.communication.ServerRpcQueue;
50  
51  /**
52   * Magnolia rich text field adds an ability to custom plugins to communicate
53   * with the server. This was not possible with the add-on out of the box.
54   */
55  public class VMagnoliaCKEditorTextField extends VCKEditorTextField implements VMagnoliaCKEditorTextEvents.Listener {
56  
57      private VMagnoliaCKEditor editor;
58      private List<String> pluginEvents;
59      private ValueMap customPlugins;
60      private boolean immediate;
61  
62      @Override
63      public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
64  
65          // Make sure external plugins are assigned before the loading command.
66          if (uidl.hasAttribute(VAR_SERVERPLUGINS)) {
67              customPlugins = uidl.getMapAttribute(VAR_SERVERPLUGINS);
68          }
69  
70          // list of plugin events that server is interested of handling.
71          if (uidl.hasAttribute(VAR_EVENTNAMES)) {
72              pluginEvents = Arrays.asList(uidl.getStringArrayAttribute(VAR_EVENTNAMES));
73          }
74  
75          if (uidl.hasAttribute(ATTR_IMMEDIATE)) {
76              immediate = uidl.getBooleanAttribute(ATTR_IMMEDIATE);
77          }
78  
79          super.updateFromUIDL(uidl, client);
80  
81          // Server wants to send an event to a plugin, we must do this after super value update.
82          if (uidl.hasAttribute(VAR_FIRE_PLUGIN_EVENT) && this.editor != null) {
83              this.editor.fire(
84                      uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT),
85                      uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE)
86              );
87          }
88      }
89  
90      @Override
91      protected CKEditor loadEditor(String inPageConfig) {
92          // Register external plugins
93          if (customPlugins != null && customPlugins.getKeySet() != null && !customPlugins.getKeySet().isEmpty()) {
94              for (String plugin : customPlugins.getKeySet()) {
95                  addExternalPlugin(plugin, customPlugins.getString(plugin));
96              }
97          }
98  
99          // Set convenience base path for registering external plugins in custom config.js
100         setVaadinDirUrl(clientToServer.getConfiguration().getVaadinDirUrl());
101 
102         // Load editor
103         editor = (VMagnoliaCKEditor) super.loadEditor(inPageConfig);
104         return editor;
105     }
106 
107     private native void setVaadinDirUrl(String vaadinDirUrl) /*-{
108         $wnd.CKEDITOR.vaadinDirUrl = vaadinDirUrl;
109     }-*/;
110 
111     private native void addExternalPlugin(String pluginName, String path) /*-{
112         $wnd.CKEDITOR.plugins.addExternal(pluginName, path, 'plugin.js');
113     }-*/;
114 
115     /**
116      * Add plugin listeners when instance is ready.
117      */
118     @Override
119     public void onInstanceReady() {
120         super.onInstanceReady();
121 
122         // Add plugin listeners
123         if (pluginEvents != null && !pluginEvents.isEmpty()) {
124             for (String eventName : pluginEvents) {
125                 editor.addPluginListener(eventName, this);
126             }
127         }
128     }
129 
130     @Override
131     public void onPluginEvent(String eventName, String data) {
132         if (pluginEvents.contains(eventName)) {
133             clientToServer.updateVariable(paintableId, VAR_EVENT_PREFIX + eventName, data == null ? "" : data, true);
134         }
135     }
136 
137     /**
138      * Override VCKEditorTextField's default behavior, defer update in case field is immediate.
139      */
140     @Override
141     public void onChange() {
142         if (editor != null && !editor.isReadOnly()) {
143             clientToServer.updateVariable(paintableId, VAR_TEXT, editor.getData(), false);
144             if (immediate) {
145                 valueUpdateTimer.schedule(200);
146             }
147         }
148     }
149 
150     private Timer valueUpdateTimer = new Timer() {
151         @Override
152         public void run() {
153             ServerRpcQueue.get(clientToServer).flush();
154         }
155     };
156 
157     @Override
158     public void doResize() {
159         super.doResize();
160     }
161 }