简体   繁体   English

GWT:如何从当前光标位置或选定的文本中获取CSS样式属性?

[英]GWT: How to get css style attributes from current cursor positon or selected text?

I had used https://code.google.com/p/google-web-toolkit/source/browse/tags/2.5.0/samples/showcase/src/com/google/gwt/sample/showcase/client/content/text/RichTextToolbar.java 我曾经使用过https://code.google.com/p/google-web-toolkit/source/browse/tags/2.5.0/samples/showcase/src/com/google/gwt/sample/showcase/client/content /text/RichTextToolbar.java

I want to show font family, font size, color and BG-Color of the text, I am getting text color through (GWT) Formatter.getForeColor(), but remaining i don't know how to get. 我想显示文本的字体,字体大小,颜色和BG颜色,我正在通过(GWT)Formatter.getForeColor()获取文本颜色,但我不知道该如何获取。

Example: <font face="Arial">Apple </font><span style="background-color: rgb(255, 0, 0);"><font face="Courier" size="5">banana</font></span><br> 例如: <font face="Arial">Apple </font><span style="background-color: rgb(255, 0, 0);"><font face="Courier" size="5">banana</font></span><br>

if cursor at in 'Apple' it should return font family is Arial, and if cursor at 'Banana' it should return font-family: Courier and size:3 and BG-Color: Red 如果光标在“ Apple”中,则应返回字体系列为Arial,如果光标在“ Banana”中,则应返回字体族:Courier and size:3和BG-Color:Red

For me no problem if the solution in JavaScript or JQuery or GWT. 对我来说,如果使用JavaScript或JQuery或GWT解决方案,则没有问题。

I want to do toolbar like Google docs. 我想做类似Google文档的工具栏。

If any one have Idea please help me how to get it? 如果有人有想法,请帮助我如何获得它?

I see two options. 我看到两个选择。 Either you build the UI using GWT components, so you can add and read the style information in the code, or you try to get the element below the mouse pointer using JSNI and then try to analyze this element. 您可以使用GWT组件构建UI,以便可以在代码中添加和读取样式信息,也可以尝试使用JSNI在鼠标指针下方获取元素,然后尝试分析该元素。

The first, probably easier, solution looks like this: 第一个可能更简单的解决方案如下所示:

public class FontTest implements EntryPoint {

    public void onModuleLoad() {
        FlowPanel panel = new FlowPanel();
        Label labelA = new Label("Apple");
        labelA.getElement().getStyle().setProperty("fontFamily", "Arial");
        Label labelB = new Label("Banana");
        labelB.getElement().getStyle().setProperty("fontFamily", "Courier");
        labelB.getElement().getStyle().setFontSize(5, Unit.EM);
        labelB.getElement().getStyle().setBackgroundColor("rgb(255, 0, 0)");
        panel.add(labelA);
        panel.add(labelB);
        labelA.addMouseOverHandler(new FontMousOverHandler());
        labelB.addMouseOverHandler(new FontMousOverHandler());
        RootLayoutPanel.get().add(panel);
    }

    private static class FontMousOverHandler implements MouseOverHandler {
        @Override
        public void onMouseOver(MouseOverEvent event) {
            Label label = (Label) event.getSource();
            String font = label.getElement().getStyle()
                    .getProperty("fontFamily");
            String color = label.getElement().getStyle().getBackgroundColor();
            PopupPanel pp = new PopupPanel(true);
            pp.add(new Label(font + " / " + color));
            pp.setPopupPosition(label.getAbsoluteLeft(), label.getAbsoluteTop());
            pp.show();
        }
    }
}

The above sample is simplified, you'd have to make your code much smarter to handle all variations of HTML attributes, CSS style-names and direct styles. 上面的示例已简化,您必须使代码更加智能,以处理HTML属性,CSS样式名称和直接样式的所有变体。

The second option is to get the element that is currently below the mouse pointer. 第二个选项是获取当前位于鼠标指针下方的元素。 This can be done using some JSNI magic: 可以使用一些JSNI魔术来完成:

    private native Element getElementFromPoint(int x, int y) /*-{
            return $wnd.document.elementFromPoint(x, y);
    }-*/;

Of course you need a trigger to call the getElementFromPoint() method. 当然,您需要一个触发器来调用getElementFromPoint()方法。 You can use a MouseHandler as well or some kind of background code (aka Timer) to trigger. 您也可以使用MouseHandler或某种背景代码(也称为Timer)来触发。 Once know where the mouse pointer is, get the element and analyze the style of element. 一旦知道鼠标指针在哪里,就获取元素并分析元素的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM