我如何更改谷歌页面编辑器使用字体真棒图标

How can I change the Google Pagedown Editor to use font awesome icons?

本文关键字:字体 图标 编辑器 何更改 谷歌      更新时间:2024-04-27

我们使用带有AngularJS指令的pagedown编辑器。我相信这与在堆栈溢出中使用的相同:

angular.module("ui.pagedown", [])
.directive("pagedownEditor", function ($compile, $timeout, $window, $q) {
    var nextId = 0;
    var converter = Markdown.getSanitizingConverter();
    Markdown.Extra.init(converter, mdExtraOptions);
    converter.hooks.chain("preBlockGamut", function (text, rbg) {
        return text.replace(/^ {0,3}""" *'n((?:.*?'n)+?) {0,3}""" *$/gm, function (whole, inner) {
            return "<blockquote>" + rbg(inner) + "</blockquote>'n";
        });
    });
    return {
        restrict: "E",
        scope: {
            content: "=",
            showPreview: "@",
            help: "&",
            insertImage: "&"
        },
        link: function (scope, element, attrs) {
            var editorUniqueId;
            if (attrs.id == null) {
                editorUniqueId = nextId++;
            } else {
                editorUniqueId = attrs.id;
            }
            // just hide the preview, we still need it for "onPreviewRefresh" hook
            var previewHiddenStyle = scope.showPreview == "false" ? "display: none;" : "";
            var newElement = $compile(
                '<div>' +
                    '<div class="wmd-panel">' +
                            '<div id="wmd-button-bar-' + editorUniqueId + '"></div>' +
                            '<textarea class="wmd-input" id="wmd-input-' + editorUniqueId + '" ng-model="content"></textarea>' +
                    '</div>' +
                    '<div id="wmd-preview-' + editorUniqueId + '" class="pagedown-preview wmd-panel wmd-preview" style="' + previewHiddenStyle + '"></div>' +
                '</div>')(scope);
            // html() doesn't work
            element.append(newElement);
            var help = angular.isFunction(scope.help) ? scope.help : function () {
                // redirect to the guide by default
                $window.open("http://daringfireball.net/projects/markdown/syntax", "_blank");
            };
            var editor = new Markdown.Editor(converter, "-" + editorUniqueId, {
                handler: help
            });
            var editorElement = angular.element(document.getElementById("wmd-input-" + editorUniqueId));
            //add watch for content
            if(scope.showPreview != "false") {
                scope.$watch('content', function () {
                    editor.refreshPreview();
                });
            }
            editor.hooks.chain("onPreviewRefresh", function() {
                // wire up changes caused by user interaction with the pagedown controls
                // and do within $apply
                $timeout(function() {
                    scope.content = editorElement.val();
                });
            });
            if (angular.isFunction(scope.insertImage)) {
                editor.hooks.set("insertImageDialog", function(callback) {
                    // expect it to return a promise or a url string
                    var result = scope.insertImage();
                    // Note that you cannot call the callback directly from the hook; you have to wait for the current scope to be exited.
                    // https://code.google.com/p/pagedown/wiki/PageDown#insertImageDialog
                    $timeout(function() {
                        if (!result) {
                            // must be null to indicate failure
                            callback(null);
                        } else {
                            // safe way to handle either string or promise
                            $q.when(result).then(
                                function success(imgUrl) {
                                    callback(imgUrl);
                                },
                                function error(reason) {
                                    callback(null);
                                }
                            );
                        }
                    });
                    return true;
                });
            }
            editor.run();
        }
    }
})
.directive("pagedownViewer", function ($compile, $sce) {
    var converter = Markdown.getSanitizingConverter();
    Markdown.Extra.init(converter, mdExtraOptions);
    return {
        restrict: "E",
        scope: {
            content: "="
        },
        link: function (scope, element, attrs) {
            var unwatch;
            var run = function run() {
                // stop continuing and watching if scope or the content is unreachable
                if (!scope || (scope.content == undefined || scope.content == null) && unwatch) {
                    unwatch();
                    return;
                }
                scope.sanitizedContent = $sce.trustAsHtml(converter.makeHtml(scope.content));
            };
            unwatch = scope.$watch("content", run);
            run();
            var newElementHtml = "<pre ng-bind-html='sanitizedContent'></pre>";
            var newElement = $compile(newElementHtml)(scope);
            element.append(newElement);
        }
    }
});

http://plnkr.co/edit/1Vxo5UfYpHQdjhWatimU?p=preview

我们想将其更改为使用字体很棒的图标集,而不是使用图标,但我们不确定如何做到这一点。

有人知道我需要做些什么来让它使用很棒的字体而不是默认的图标吗。

谢谢,

当前PageDown编辑器不支持对按钮栏呈现进行任何自定义。

所以你只剩下三个选项:

  • 您只需创建自己的按钮图像文件,并将其用于图标。然而,在这种情况下,你正在失去使用字体真棒的好处
  • 分叉并向Markdown.Editor.js添加必要的修改
  • 您可以在编辑器呈现后更改生成的HTML,删除旧按钮并添加新按钮,但有些功能不起作用,稍后会详细介绍

解决方案1:创建自定义按钮图像

PageDown编辑器使用一个精灵图像来存储所有按钮及其悬停和禁用状态。所以你可以修改它以拥有你的自定义按钮。

然后你只需要更改你的CSS来使用图标(这里我使用了SO编辑器按钮):

.wmd-button > span {
    background-image: url("http://cdn.sstatic.net/stackoverflow/img/wmd-buttons.svg");
}

Plunker

解决方案2:分叉Markdown.Editor.js

本质上你必须改变两种方法:

  • 生成CCD_ 4并应用背景定位的CCD_ 3函数(PageDown编辑器使用大图像来包含所有按钮及其状态)。这里你只需要添加FA类
  • 处理事件连线、悬停效果和禁用状态的setupButton。在这里,您需要删除已启用的状态处理或可选的悬停处理,因为这也可以在angular-pagedown.css中完成

所以makeButton的代码看起来像:

var makeButton = function (id, title, XShift, textOp, faclass) {
        var button = document.createElement("li");
        button.className = "wmd-button";
        button.style.left = xPosition + "px";
        xPosition += 25;
        var buttonImage = document.createElement("span");
        buttonImage.className = "fa " + faclass;
        button.id = id + postfix;
        button.appendChild(buttonImage);
        button.title = title;
        button.XShift = XShift;
        if (textOp)
            button.textOp = textOp;
        setupButton(button, true);
        buttonRow.appendChild(button);
        return button;
    };

setupButton看起来是这样的:

    function setupButton(button, isEnabled) {
        var image = button.getElementsByTagName("span")[0];
        if (isEnabled) {
            image.style.color = "#000000"
            if (!button.isHelp) {
                button.onclick = function () {
                    doClick(this);
                    return false;
                }
            }
        }
        else {
            image.style.color = "#c5c5c5"
            button.onmouseover = button.onmouseout = button.onclick = function () { };
        }
    }

最后,您必须修改makeButton的调用站点,以便在创建按钮时包含所需的字体真棒类:

buttons.bold = makeButton("wmd-bold-button", getString("bold"), "0px", bindCommand("doBold"), "fa-bold");
//...

在CSS方面,你需要添加悬停效果并删除背景:

.wmd-button > span {
    text-align: center;
    width: 20px;
    height: 20px;
    display: inline-block;
}
.wmd-button > span:hover {
  background: lightskyblue;
}

Plunker

解决方案3:更改生成的HTML:

此解决方案不需要更改Markdown.Editor,但启用/禁用状态处理不起作用。

基本上,您必须在editor.run();:之后的角度指令中,将字体真棒类添加到生成的span:中

 var icons = [
          {md: "wmd-bold-button", fa: "fa-bold"},
          //...
          ]
        icons.forEach(function(i) {
          var e = angular.element(document.getElementById(i.md + "-" + editorUniqueId));
          e.find("span").addClass("fa "+ i.fa);
        });

在这种情况下,您还需要稍微更改CSS:

.wmd-button > span {
     width: 20px;
    height: 20px;
}
.wmd-button > span:hover {
    background: #99cafa;
    width: 20px;
    height: 20px;
}

Plunker

您可以使用字体很棒的PageDown库