You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VRichTextArea.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.client.ui;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.Map.Entry;
  20. import com.google.gwt.core.client.Scheduler;
  21. import com.google.gwt.event.dom.client.BlurHandler;
  22. import com.google.gwt.event.dom.client.KeyDownEvent;
  23. import com.google.gwt.event.dom.client.KeyDownHandler;
  24. import com.google.gwt.event.dom.client.KeyPressEvent;
  25. import com.google.gwt.event.dom.client.KeyPressHandler;
  26. import com.google.gwt.event.shared.HandlerRegistration;
  27. import com.google.gwt.user.client.Command;
  28. import com.google.gwt.user.client.Timer;
  29. import com.google.gwt.user.client.ui.Composite;
  30. import com.google.gwt.user.client.ui.FlowPanel;
  31. import com.google.gwt.user.client.ui.Focusable;
  32. import com.google.gwt.user.client.ui.HTML;
  33. import com.google.gwt.user.client.ui.RichTextArea;
  34. import com.google.gwt.user.client.ui.Widget;
  35. import com.vaadin.client.ApplicationConnection;
  36. import com.vaadin.client.BrowserInfo;
  37. import com.vaadin.client.ConnectorMap;
  38. import com.vaadin.client.ui.Field;
  39. import com.vaadin.client.ui.ShortcutActionHandler;
  40. import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
  41. import com.vaadin.client.ui.TouchScrollDelegate;
  42. import com.vaadin.v7.client.ui.richtextarea.VRichTextToolbar;
  43. /**
  44. * This class implements a basic client side rich text editor component.
  45. *
  46. * @author Vaadin Ltd.
  47. *
  48. */
  49. public class VRichTextArea extends Composite
  50. implements Field, KeyPressHandler, KeyDownHandler, Focusable {
  51. /**
  52. * The input node CSS classname.
  53. */
  54. public static final String CLASSNAME = "v-richtextarea";
  55. /** For internal use only. May be removed or replaced in the future. */
  56. public String id;
  57. /** For internal use only. May be removed or replaced in the future. */
  58. public ApplicationConnection client;
  59. /** For internal use only. May be removed or replaced in the future. */
  60. public boolean immediate = false;
  61. /** For internal use only. May be removed or replaced in the future. */
  62. public RichTextArea rta;
  63. /** For internal use only. May be removed or replaced in the future. */
  64. public VRichTextToolbar formatter;
  65. /** For internal use only. May be removed or replaced in the future. */
  66. public HTML html = new HTML();
  67. private final FlowPanel fp = new FlowPanel();
  68. private boolean enabled = true;
  69. /** For internal use only. May be removed or replaced in the future. */
  70. public int maxLength = -1;
  71. private int toolbarNaturalWidth = 500;
  72. /** For internal use only. May be removed or replaced in the future. */
  73. public HandlerRegistration keyPressHandler;
  74. private ShortcutActionHandlerOwner hasShortcutActionHandler;
  75. private boolean readOnly = false;
  76. private final Map<BlurHandler, HandlerRegistration> blurHandlers = new HashMap<BlurHandler, HandlerRegistration>();
  77. public VRichTextArea() {
  78. createRTAComponents();
  79. fp.add(formatter);
  80. fp.add(rta);
  81. initWidget(fp);
  82. setStyleName(CLASSNAME);
  83. TouchScrollDelegate.enableTouchScrolling(html, html.getElement());
  84. }
  85. private void createRTAComponents() {
  86. rta = new RichTextArea();
  87. rta.setWidth("100%");
  88. rta.addKeyDownHandler(this);
  89. formatter = new VRichTextToolbar(rta);
  90. // Add blur handlers
  91. for (Entry<BlurHandler, HandlerRegistration> handler : blurHandlers
  92. .entrySet()) {
  93. // Remove old registration
  94. handler.getValue().removeHandler();
  95. // Add blur handlers
  96. addBlurHandler(handler.getKey());
  97. }
  98. }
  99. public void setEnabled(boolean enabled) {
  100. if (this.enabled != enabled) {
  101. // rta.setEnabled(enabled);
  102. swapEditableArea();
  103. this.enabled = enabled;
  104. }
  105. }
  106. /**
  107. * Swaps html to rta and visa versa.
  108. */
  109. private void swapEditableArea() {
  110. String value = getValue();
  111. if (html.isAttached()) {
  112. fp.remove(html);
  113. if (BrowserInfo.get().isWebkit()) {
  114. fp.remove(formatter);
  115. createRTAComponents(); // recreate new RTA to bypass #5379
  116. fp.add(formatter);
  117. }
  118. fp.add(rta);
  119. } else {
  120. fp.remove(rta);
  121. fp.add(html);
  122. }
  123. setValue(value);
  124. }
  125. /** For internal use only. May be removed or replaced in the future. */
  126. public void selectAll() {
  127. /*
  128. * There is a timing issue if trying to select all immediately on first
  129. * render. Simple deferred command is not enough. Using Timer with
  130. * moderated timeout. If this appears to fail on many (most likely slow)
  131. * environments, consider increasing the timeout.
  132. *
  133. * FF seems to require the most time to stabilize its RTA. On Vaadin
  134. * tiergarden test machines, 200ms was not enough always (about 50%
  135. * success rate) - 300 ms was 100% successful. This however was not
  136. * enough on a sluggish old non-virtualized XP test machine. A bullet
  137. * proof solution would be nice, GWT 2.1 might however solve these. At
  138. * least setFocus has a workaround for this kind of issue.
  139. */
  140. new Timer() {
  141. @Override
  142. public void run() {
  143. rta.getFormatter().selectAll();
  144. }
  145. }.schedule(320);
  146. }
  147. public void setReadOnly(boolean b) {
  148. if (isReadOnly() != b) {
  149. swapEditableArea();
  150. readOnly = b;
  151. }
  152. // reset visibility in case enabled state changed and the formatter was
  153. // recreated
  154. formatter.setVisible(!readOnly);
  155. }
  156. private boolean isReadOnly() {
  157. return readOnly;
  158. }
  159. @Override
  160. public void setHeight(String height) {
  161. super.setHeight(height);
  162. if (height == null || height.equals("")) {
  163. rta.setHeight("");
  164. }
  165. }
  166. @Override
  167. public void setWidth(String width) {
  168. if (width.equals("")) {
  169. /*
  170. * IE cannot calculate the width of the 100% iframe correctly if
  171. * there is no width specified for the parent. In this case we would
  172. * use the toolbar but IE cannot calculate the width of that one
  173. * correctly either in all cases. So we end up using a default width
  174. * for a RichTextArea with no width definition in all browsers (for
  175. * compatibility).
  176. */
  177. super.setWidth(toolbarNaturalWidth + "px");
  178. } else {
  179. super.setWidth(width);
  180. }
  181. }
  182. @Override
  183. public void onKeyPress(KeyPressEvent event) {
  184. if (maxLength >= 0) {
  185. Scheduler.get().scheduleDeferred(new Command() {
  186. @Override
  187. public void execute() {
  188. if (rta.getHTML().length() > maxLength) {
  189. rta.setHTML(rta.getHTML().substring(0, maxLength));
  190. }
  191. }
  192. });
  193. }
  194. }
  195. @Override
  196. public void onKeyDown(KeyDownEvent event) {
  197. // delegate to closest shortcut action handler
  198. // throw event from the iframe forward to the shortcuthandler
  199. ShortcutActionHandler shortcutHandler = getShortcutHandlerOwner()
  200. .getShortcutActionHandler();
  201. if (shortcutHandler != null) {
  202. shortcutHandler.handleKeyboardEvent(
  203. com.google.gwt.user.client.Event.as(event.getNativeEvent()),
  204. ConnectorMap.get(client).getConnector(this));
  205. }
  206. }
  207. private ShortcutActionHandlerOwner getShortcutHandlerOwner() {
  208. if (hasShortcutActionHandler == null) {
  209. Widget parent = getParent();
  210. while (parent != null) {
  211. if (parent instanceof ShortcutActionHandlerOwner) {
  212. break;
  213. }
  214. parent = parent.getParent();
  215. }
  216. hasShortcutActionHandler = (ShortcutActionHandlerOwner) parent;
  217. }
  218. return hasShortcutActionHandler;
  219. }
  220. @Override
  221. public int getTabIndex() {
  222. return rta.getTabIndex();
  223. }
  224. @Override
  225. public void setAccessKey(char key) {
  226. rta.setAccessKey(key);
  227. }
  228. @Override
  229. public void setFocus(boolean focused) {
  230. /*
  231. * Similar issue as with selectAll. Focusing must happen before possible
  232. * selectall, so keep the timeout here lower.
  233. */
  234. new Timer() {
  235. @Override
  236. public void run() {
  237. rta.setFocus(true);
  238. }
  239. }.schedule(300);
  240. }
  241. @Override
  242. public void setTabIndex(int index) {
  243. rta.setTabIndex(index);
  244. }
  245. /**
  246. * Set the value of the text area.
  247. *
  248. * @param value
  249. * The text value. Can be html.
  250. */
  251. public void setValue(String value) {
  252. if (rta.isAttached()) {
  253. rta.setHTML(value);
  254. } else {
  255. html.setHTML(value);
  256. }
  257. }
  258. /**
  259. * Get the value the text area.
  260. */
  261. public String getValue() {
  262. if (rta.isAttached()) {
  263. return rta.getHTML();
  264. } else {
  265. return html.getHTML();
  266. }
  267. }
  268. /**
  269. * Browsers differ in what they return as the content of a visually empty
  270. * rich text area. This method is used to normalize these to an empty
  271. * string. See #8004.
  272. *
  273. * @return cleaned html string
  274. */
  275. public String getSanitizedValue() {
  276. BrowserInfo browser = BrowserInfo.get();
  277. String result = getValue();
  278. if (browser.isFirefox()) {
  279. if ("<br>".equals(result)) {
  280. result = "";
  281. }
  282. } else if (browser.isWebkit() || browser.isEdge()) {
  283. if ("<br>".equals(result) || "<div><br></div>".equals(result)) {
  284. result = "";
  285. }
  286. } else if (browser.isIE()) {
  287. if ("<P>&nbsp;</P>".equals(result)) {
  288. result = "";
  289. }
  290. } else if (browser.isOpera()) {
  291. if ("<br>".equals(result) || "<p><br></p>".equals(result)) {
  292. result = "";
  293. }
  294. }
  295. return result;
  296. }
  297. /**
  298. * Adds a blur handler to the component.
  299. *
  300. * @param blurHandler
  301. * the blur handler to add
  302. */
  303. public void addBlurHandler(BlurHandler blurHandler) {
  304. blurHandlers.put(blurHandler, rta.addBlurHandler(blurHandler));
  305. }
  306. /**
  307. * Removes a blur handler.
  308. *
  309. * @param blurHandler
  310. * the handler to remove
  311. */
  312. public void removeBlurHandler(BlurHandler blurHandler) {
  313. HandlerRegistration registration = blurHandlers.remove(blurHandler);
  314. if (registration != null) {
  315. registration.removeHandler();
  316. }
  317. }
  318. }