Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Last active July 13, 2018 15:48
Show Gist options
  • Save branflake2267/42ef071a9b9f7d9083f86c644679cf5c to your computer and use it in GitHub Desktop.
Save branflake2267/42ef071a9b9f7d9083f86c644679cf5c to your computer and use it in GitHub Desktop.
GXT/GWT 2.8.2 - RichTextArea override/workaround for Microsoft (MS) Edge where the text background/highlight color doesn't work.
/*
* Copyright 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.user.client.ui.impl;
import com.sencha.gxt.core.client.GXT;
/**
* Safari rich text platform implementation.
*/
public class RichTextAreaImplSafari extends RichTextAreaImplStandard {
@Override
public void setBackColor(String color) {
// Webkit uses 'BackColor' for the *entire area's* background. 'HiliteColor'
// does what we actually want.
// ~~~ workaround - ms edge expects the color to have a # when it's hex.
if (GXT.isMSEdge()) {
color = "#" + color;
}
execCommand("HiliteColor", color);
}
@Override
protected native String getTextImpl() /*-{
return [email protected]::elem.contentWindow.document.body.innerText;
}-*/;
@Override
protected native void hookEvents() /*-{
var elem = [email protected]::elem;
var wnd = elem.contentWindow;
elem.__gwt_handler = function(evt) {
@com.google.gwt.user.client.DOM::dispatchEvent(Lcom/google/gwt/user/client/Event;Lcom/google/gwt/dom/client/Element;)(evt, elem);
};
wnd.addEventListener('keydown', elem.__gwt_handler, true);
wnd.addEventListener('keyup', elem.__gwt_handler, true);
wnd.addEventListener('keypress', elem.__gwt_handler, true);
wnd.addEventListener('mousedown', elem.__gwt_handler, true);
wnd.addEventListener('mouseup', elem.__gwt_handler, true);
wnd.addEventListener('mousemove', elem.__gwt_handler, true);
wnd.addEventListener('mouseover', elem.__gwt_handler, true);
wnd.addEventListener('mouseout', elem.__gwt_handler, true);
wnd.addEventListener('click', elem.__gwt_handler, true);
// Focus/blur event handlers. For some reason, [add|remove]eventListener()
// doesn't work on the iframe element (at least not for focus/blur). Don't
// dispatch through the normal handler method, as some of the querying we do
// there interferes with focus.
wnd.onfocus = function(evt) {
@com.google.gwt.user.client.DOM::dispatchEvent(Lcom/google/gwt/user/client/Event;Lcom/google/gwt/dom/client/Element;)(evt, elem);
};
wnd.onblur = function(evt) {
@com.google.gwt.user.client.DOM::dispatchEvent(Lcom/google/gwt/user/client/Event;Lcom/google/gwt/dom/client/Element;)(evt, elem);
};
}-*/;
@Override
protected native void setTextImpl(String text) /*-{
[email protected]::elem.contentWindow.document.body.innerText = text;
}-*/;
@Override
protected native void unhookEvents() /*-{
var elem = [email protected]::elem;
var wnd = elem.contentWindow;
wnd.removeEventListener('keydown', elem.__gwt_handler, true);
wnd.removeEventListener('keyup', elem.__gwt_handler, true);
wnd.removeEventListener('keypress', elem.__gwt_handler, true);
wnd.removeEventListener('mousedown', elem.__gwt_handler, true);
wnd.removeEventListener('mouseup', elem.__gwt_handler, true);
wnd.removeEventListener('mousemove', elem.__gwt_handler, true);
wnd.removeEventListener('mouseover', elem.__gwt_handler, true);
wnd.removeEventListener('mouseout', elem.__gwt_handler, true);
wnd.removeEventListener('click', elem.__gwt_handler, true);
elem.__gwt_handler = null;
elem.onfocus = null;
elem.onblur = null;
}-*/;
}
@branflake2267
Copy link
Author

The RichTextarea execCommand("HiliteColor", color); doesn't function properly on Microsoft Edge. So a # is added to the hex color code.

@branflake2267
Copy link
Author

Using this workaround:

  • Copy this code to your source folder and keep the same package name stated at the top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment