Как изменить цвет HTMLeditor с кодом? - PullRequest
0 голосов
/ 07 января 2019

Я сделал собственный htmleditor, который расширяет HTMLEditor. Я удалил несколько кнопок и добавил несколько.

Теперь я хочу добавить пару кнопок, которые изменяют текст, набранный на 1 определенный цвет. Но я не могу найти способ изменить цвет текста в setOnAction кнопки. Я попробовал пару способов.

1: HTML, который я вставляю в позицию курсора

2: установка палитры цветов htmleditor

(строка jsCodeInsertHtml допускает HTML в курсоре)

    String jsCodeInsertHtml = "function insertHtmlAtCursor(html) {\n" +
            "    var range, node;\n" +
            "    if (window.getSelection && window.getSelection().getRangeAt) {\n" +
            "        range = window.getSelection().getRangeAt(0);\n" +
            "        node = range.createContextualFragment(html);\n" +
            "        range.insertNode(node);\n" +
            "    } else if (document.selection && document.selection.createRange) {\n" +
            "        document.selection.createRange().pasteHTML(html);\n" +
            "    }\n" +
            "}insertHtmlAtCursor('####html####')";



          // add a custom button to the top toolbar.
            Node nodetop = editor.lookup(".top-toolbar");
            if (nodetop instanceof ToolBar) {
                ToolBar topbar = (ToolBar) nodetop;



                //var1 color
                ImageView graphic = new ImageView(var1pic);
                Button colorVar1Button = new Button("", graphic);
                colorVar1Button.setFocusTraversable(false);
                colorVar1Button.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {
                        WebView webView=(WebView)editor.lookup("WebView");
                        WebEngine engine= webView.getEngine();

                        //try 1
                        engine.executeScript(jsCodeInsertHtml.replace("####html####","<p><span style=\"color: rgb(200, 200, 200); caret-color: rgb(200, 200, 200);\">"));
                         //the HTML is inserted , but no colorchange. i tryed some htmlvariants aswel


                        //try 2
                        Node color = editor.lookup(".html-editor-foreground");
                        ((ColorPicker)color).setValue(Color.rgb(200,200,200));
                        // i notice the button change in the colorpicker ,
                        // but no change in color happens

                topbar.getItems().addAll(colorVar1Button);

1 Ответ

0 голосов
/ 13 января 2019

Нашел решение. мне просто нужно было запустить событие Collorpicker, чтобы оно заработало

                colorVar1Button.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {
                        Node color = editor.lookup(".html-editor-foreground");

                        ((ColorPicker)color).setValue(Color.HOTPINK);
                        color.fireEvent(new ActionEvent());
                    }
                });

            topbar.getItems().addAll(colorVar1Button);
...