Path: blob/trunk/third_party/closure/goog/ui/editor/defaulttoolbar.js
4058 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Factory functions for creating a default editing toolbar.8*9* @see ../../demos/editor/editor.html10*/1112goog.provide('goog.ui.editor.ButtonDescriptor');13goog.provide('goog.ui.editor.DefaultToolbar');1415goog.require('goog.asserts');16goog.require('goog.dom');17goog.require('goog.dom.TagName');18goog.require('goog.dom.classlist');19goog.require('goog.editor.Command');20goog.require('goog.style');21goog.require('goog.ui.editor.ToolbarFactory');22goog.require('goog.ui.editor.messages');23goog.require('goog.userAgent');24goog.requireType('goog.ui.Button');25goog.requireType('goog.ui.ButtonRenderer');26goog.requireType('goog.ui.ColorMenuButtonRenderer');27goog.requireType('goog.ui.Control');28goog.requireType('goog.ui.ControlContent');29goog.requireType('goog.ui.MenuButtonRenderer');30goog.requireType('goog.ui.MenuItem');31goog.requireType('goog.ui.Select');32goog.requireType('goog.ui.Toolbar');33goog.requireType('goog.ui.ToolbarColorMenuButton');3435// Font menu creation.363738/** @desc Font menu item caption for the default sans-serif font. */39goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL = goog.getMsg('Normal');404142/** @desc Font menu item caption for the default serif font. */43goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL_SERIF =44goog.getMsg('Normal / serif');454647/**48* Common font descriptors for all locales. Each descriptor has the following49* attributes:50* <ul>51* <li>`caption` - Caption to show in the font menu (e.g. 'Tahoma')52* <li>`value` - Value for the corresponding 'font-family' CSS style53* (e.g. 'Tahoma, Arial, sans-serif')54* </ul>55* @type {!Array<{caption:string, value:string}>}56* @private57*/58goog.ui.editor.DefaultToolbar.FONTS_ = [59{60caption: goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL,61value: 'arial,sans-serif'62},63{64caption: goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL_SERIF,65value: 'times new roman,serif'66},67{caption: 'Courier New', value: 'courier new,monospace'},68{caption: 'Georgia', value: 'georgia,serif'},69{caption: 'Trebuchet', value: 'trebuchet ms,sans-serif'},70{caption: 'Verdana', value: 'verdana,sans-serif'}71];727374/**75* Locale-specific font descriptors. The object is a map of locale strings to76* arrays of font descriptors.77* @type {!Object<!Array<{caption:string, value:string}>>}78* @private79*/80goog.ui.editor.DefaultToolbar.I18N_FONTS_ = {81'ja': [82{83caption: '\uff2d\uff33 \uff30\u30b4\u30b7\u30c3\u30af',84value: 'ms pgothic,sans-serif'85},86{caption: '\uff2d\uff33 \uff30\u660e\u671d', value: 'ms pmincho,serif'}, {87caption: '\uff2d\uff33 \u30b4\u30b7\u30c3\u30af',88value: 'ms gothic,monospace'89}90],91'ko': [92{caption: '\uad74\ub9bc', value: 'gulim,sans-serif'},93{caption: '\ubc14\ud0d5', value: 'batang,serif'},94{caption: '\uad74\ub9bc\uccb4', value: 'gulimche,monospace'}95],96'zh-tw': [97{caption: '\u65b0\u7d30\u660e\u9ad4', value: 'pmingliu,serif'},98{caption: '\u7d30\u660e\u9ad4', value: 'mingliu,serif'}99],100'zh-cn': [101{caption: '\u5b8b\u4f53', value: 'simsun,serif'},102{caption: '\u9ed1\u4f53', value: 'simhei,sans-serif'},103{caption: 'MS Song', value: 'ms song,monospace'}104]105};106107108/**109* Default locale for font names.110* @type {string}111* @private112*/113goog.ui.editor.DefaultToolbar.locale_ = 'en-us';114115116/**117* Sets the locale for the font names. If not set, defaults to 'en-us'.118* Used only for default creation of font names name. Must be set119* before font name menu is created.120* @param {string} locale Locale to use for the toolbar font names.121*/122goog.ui.editor.DefaultToolbar.setLocale = function(locale) {123'use strict';124goog.ui.editor.DefaultToolbar.locale_ = locale;125};126127128/**129* Initializes the given font menu button by adding default fonts to the menu.130* If goog.ui.editor.DefaultToolbar.setLocale was called to specify a locale131* for which locale-specific default fonts exist, those are added before132* common fonts.133* @param {!goog.ui.Select} button Font menu button.134*/135goog.ui.editor.DefaultToolbar.addDefaultFonts = function(button) {136'use strict';137// Normalize locale to lowercase, with a hyphen (see bug 1036165).138const locale =139goog.ui.editor.DefaultToolbar.locale_.replace(/_/, '-').toLowerCase();140// Add locale-specific default fonts, if any.141let fontlist = [];142143if (locale in goog.ui.editor.DefaultToolbar.I18N_FONTS_) {144fontlist = goog.ui.editor.DefaultToolbar.I18N_FONTS_[locale];145}146if (fontlist.length) {147goog.ui.editor.ToolbarFactory.addFonts(button, fontlist);148}149// Add locale-independent default fonts.150goog.ui.editor.ToolbarFactory.addFonts(151button, goog.ui.editor.DefaultToolbar.FONTS_);152};153154155// Font size menu creation.156157158/** @desc Font size menu item caption for the 'Small' size. */159goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_SMALL = goog.getMsg('Small');160161162/** @desc Font size menu item caption for the 'Normal' size. */163goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_NORMAL = goog.getMsg('Normal');164165166/** @desc Font size menu item caption for the 'Large' size. */167goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_LARGE = goog.getMsg('Large');168169170/** @desc Font size menu item caption for the 'Huge' size. */171goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_HUGE = goog.getMsg('Huge');172173174/**175* Font size descriptors, each with the following attributes:176* <ul>177* <li>`caption` - Caption to show in the font size menu (e.g. 'Huge')178* <li>`value` - Value for the corresponding HTML font size (e.g. 6)179* </ul>180* @type {!Array<{caption:string, value:number}>}181* @private182*/183goog.ui.editor.DefaultToolbar.FONT_SIZES_ = [184{caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_SMALL, value: 1},185{caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_NORMAL, value: 2},186{caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_LARGE, value: 4},187{caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_HUGE, value: 6}188];189190191/**192* Initializes the given font size menu button by adding default font sizes to193* it.194* @param {!goog.ui.Select} button Font size menu button.195*/196goog.ui.editor.DefaultToolbar.addDefaultFontSizes = function(button) {197'use strict';198goog.ui.editor.ToolbarFactory.addFontSizes(199button, goog.ui.editor.DefaultToolbar.FONT_SIZES_);200};201202203// Header format menu creation.204205206/** @desc Caption for "Heading" block format option. */207goog.ui.editor.DefaultToolbar.MSG_FORMAT_HEADING = goog.getMsg('Heading');208209210/** @desc Caption for "Subheading" block format option. */211goog.ui.editor.DefaultToolbar.MSG_FORMAT_SUBHEADING = goog.getMsg('Subheading');212213214/** @desc Caption for "Minor heading" block format option. */215goog.ui.editor.DefaultToolbar.MSG_FORMAT_MINOR_HEADING =216goog.getMsg('Minor heading');217218219/** @desc Caption for "Normal" block format option. */220goog.ui.editor.DefaultToolbar.MSG_FORMAT_NORMAL = goog.getMsg('Normal');221222223/**224* Format option descriptors, each with the following attributes:225* <ul>226* <li>`caption` - Caption to show in the menu (e.g. 'Minor heading')227* <li>`command` - Corresponding {@link goog.dom.TagName} (e.g.228* 'H4')229* </ul>230* @type {!Array<{caption: string, command: !goog.dom.TagName}>}231* @private232*/233goog.ui.editor.DefaultToolbar.FORMAT_OPTIONS_ = [234{235caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_HEADING,236command: goog.dom.TagName.H2237},238{239caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_SUBHEADING,240command: goog.dom.TagName.H3241},242{243caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_MINOR_HEADING,244command: goog.dom.TagName.H4245},246{247caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_NORMAL,248command: goog.dom.TagName.P249}250];251252253/**254* Initializes the given "Format block" menu button by adding default format255* options to the menu.256* @param {!goog.ui.Select} button "Format block" menu button.257*/258goog.ui.editor.DefaultToolbar.addDefaultFormatOptions = function(button) {259'use strict';260goog.ui.editor.ToolbarFactory.addFormatOptions(261button, goog.ui.editor.DefaultToolbar.FORMAT_OPTIONS_);262};263264265/**266* Creates a {@link goog.ui.Toolbar} containing a default set of editor267* toolbar buttons, and renders it into the given parent element.268* @param {!Element} elem Toolbar parent element.269* @param {boolean=} opt_isRightToLeft Whether the editor chrome is270* right-to-left; defaults to the directionality of the toolbar parent271* element.272* @return {!goog.ui.Toolbar} Default editor toolbar, rendered into the given273* parent element.274* @see goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS275*/276goog.ui.editor.DefaultToolbar.makeDefaultToolbar = function(277elem, opt_isRightToLeft) {278'use strict';279const isRightToLeft = opt_isRightToLeft || goog.style.isRightToLeft(elem);280const buttons = isRightToLeft ?281goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS_RTL :282goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS;283return goog.ui.editor.DefaultToolbar.makeToolbar(284buttons, elem, opt_isRightToLeft);285};286287288/**289* Creates a {@link goog.ui.Toolbar} containing the specified set of290* toolbar buttons, and renders it into the given parent element. Each291* item in the `items` array must either be a292* {@link goog.editor.Command} (to create a built-in button) or a subclass293* of {@link goog.ui.Control} (to create a custom control).294* @param {!Array<string|goog.ui.Control>} items Toolbar items; each must295* be a {@link goog.editor.Command} or a {@link goog.ui.Control}.296* @param {!Element} elem Toolbar parent element.297* @param {boolean=} opt_isRightToLeft Whether the editor chrome is298* right-to-left; defaults to the directionality of the toolbar parent299* element.300* @return {!goog.ui.Toolbar} Editor toolbar, rendered into the given parent301* element.302*/303goog.ui.editor.DefaultToolbar.makeToolbar = function(304items, elem, opt_isRightToLeft) {305'use strict';306const domHelper = goog.dom.getDomHelper(elem);307const controls = [];308309for (let i = 0, button; button = items[i]; i++) {310if (typeof button === 'string') {311button = goog.ui.editor.DefaultToolbar.makeBuiltInToolbarButton(312button, domHelper);313}314if (button) {315controls.push(button);316}317}318319return goog.ui.editor.ToolbarFactory.makeToolbar(320controls, elem, opt_isRightToLeft);321};322323324/**325* Creates an instance of a subclass of {@link goog.ui.Button} for the given326* {@link goog.editor.Command}, or null if no built-in button exists for the327* command. Note that this function is only intended to create built-in328* buttons; please don't try to hack it!329* @param {string} command Editor command ID.330* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM331* creation; defaults to the current document if unspecified.332* @return {goog.ui.Button} Toolbar button (null if no built-in button exists333* for the command).334*/335goog.ui.editor.DefaultToolbar.makeBuiltInToolbarButton = function(336command, opt_domHelper) {337'use strict';338let button = null;339const descriptor = goog.ui.editor.DefaultToolbar.buttons_[command];340if (descriptor) {341// Default the factory method to makeToggleButton, since most built-in342// toolbar buttons are toggle buttons. See also343// goog.ui.editor.DefaultToolbar.button_list_.344/** @type {!Function} */345const factory =346descriptor.factory || goog.ui.editor.ToolbarFactory.makeToggleButton;347const id = descriptor.command;348const tooltip = descriptor.tooltip;349const caption = descriptor.caption;350const classNames = descriptor.classes;351// Default the DOM helper to the one for the current document.352const domHelper = opt_domHelper || goog.dom.getDomHelper();353// Instantiate the button based on the descriptor.354button = factory(id, tooltip, caption, classNames, null, domHelper);355// If this button's state should be queried when updating the toolbar,356// set the button object's queryable property to true.357if (descriptor.queryable) {358button.queryable = true;359}360}361return button;362};363364365/**366* A set of built-in buttons to display in the default editor toolbar.367* @type {!Array<string>}368*/369goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS = [370goog.editor.Command.IMAGE, goog.editor.Command.LINK, goog.editor.Command.BOLD,371goog.editor.Command.ITALIC, goog.editor.Command.UNORDERED_LIST,372goog.editor.Command.FONT_COLOR, goog.editor.Command.FONT_FACE,373goog.editor.Command.FONT_SIZE, goog.editor.Command.JUSTIFY_LEFT,374goog.editor.Command.JUSTIFY_CENTER, goog.editor.Command.JUSTIFY_RIGHT,375goog.editor.Command.EDIT_HTML376];377378379/**380* A set of built-in buttons to display in the default editor toolbar when381* the editor chrome is right-to-left (BiDi mode only).382* @type {!Array<string>}383*/384goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS_RTL = [385goog.editor.Command.IMAGE, goog.editor.Command.LINK, goog.editor.Command.BOLD,386goog.editor.Command.ITALIC, goog.editor.Command.UNORDERED_LIST,387goog.editor.Command.FONT_COLOR, goog.editor.Command.FONT_FACE,388goog.editor.Command.FONT_SIZE, goog.editor.Command.JUSTIFY_RIGHT,389goog.editor.Command.JUSTIFY_CENTER, goog.editor.Command.JUSTIFY_LEFT,390goog.editor.Command.DIR_RTL, goog.editor.Command.DIR_LTR,391goog.editor.Command.EDIT_HTML392];393394395/**396* Creates a toolbar button with the given ID, tooltip, and caption. Applies397* any custom CSS class names to the button's caption element. This button398* is designed to be used as the RTL button.399* @param {string} id Button ID; must equal a {@link goog.editor.Command} for400* built-in buttons, anything else for custom buttons.401* @param {string} tooltip Tooltip to be shown on hover.402* @param {goog.ui.ControlContent} caption Button caption.403* @param {string=} opt_classNames CSS class name(s) to apply to the caption404* element.405* @param {goog.ui.ButtonRenderer=} opt_renderer Button renderer; defaults to406* {@link goog.ui.ToolbarButtonRenderer} if unspecified.407* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM408* creation; defaults to the current document if unspecified.409* @return {!goog.ui.Button} A toolbar button.410* @private411* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration412*/413goog.ui.editor.DefaultToolbar.rtlButtonFactory_ = function(414id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {415'use strict';416const button = goog.ui.editor.ToolbarFactory.makeToggleButton(417id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);418button.updateFromValue = function(value) {419'use strict';420// Enable/disable right-to-left text editing mode in the toolbar.421const isRtl = !!value;422// Enable/disable a marker class on the toolbar's root element; the rest is423// done using CSS scoping in editortoolbar.css. This changes424// direction-senitive toolbar icons (like indent/outdent)425goog.dom.classlist.enable(426goog.asserts.assert(button.getParent().getElement()),427goog.getCssName('tr-rtl-mode'), isRtl);428button.setChecked(isRtl);429};430return button;431};432433434/**435* Creates a toolbar button with the given ID, tooltip, and caption. Applies436* any custom CSS class names to the button's caption element. Designed to437* be used to create undo and redo buttons.438* @param {string} id Button ID; must equal a {@link goog.editor.Command} for439* built-in buttons, anything else for custom buttons.440* @param {string} tooltip Tooltip to be shown on hover.441* @param {goog.ui.ControlContent} caption Button caption.442* @param {string=} opt_classNames CSS class name(s) to apply to the caption443* element.444* @param {goog.ui.ButtonRenderer=} opt_renderer Button renderer; defaults to445* {@link goog.ui.ToolbarButtonRenderer} if unspecified.446* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM447* creation; defaults to the current document if unspecified.448* @return {!goog.ui.Button} A toolbar button.449* @private450* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration451*/452goog.ui.editor.DefaultToolbar.undoRedoButtonFactory_ = function(453id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {454'use strict';455const button = goog.ui.editor.ToolbarFactory.makeButton(456id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);457button.updateFromValue = function(value) {458'use strict';459button.setEnabled(value);460};461return button;462};463464465/**466* Creates a toolbar button with the given ID, tooltip, and caption. Applies467* any custom CSS class names to the button's caption element. Used to create468* a font face button, filled with default fonts.469* @param {string} id Button ID; must equal a {@link goog.editor.Command} for470* built-in buttons, anything else for custom buttons.471* @param {string} tooltip Tooltip to be shown on hover.472* @param {goog.ui.ControlContent} caption Button caption.473* @param {string=} opt_classNames CSS class name(s) to apply to the caption474* element.475* @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer; defaults476* to {@link goog.ui.ToolbarMenuButtonRenderer} if unspecified.477* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM478* creation; defaults to the current document if unspecified.479* @return {!goog.ui.Button} A toolbar button.480* @private481* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration482*/483goog.ui.editor.DefaultToolbar.fontFaceFactory_ = function(484id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {485'use strict';486const button = goog.ui.editor.ToolbarFactory.makeSelectButton(487id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);488goog.ui.editor.DefaultToolbar.addDefaultFonts(button);489button.setDefaultCaption(goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL);490// Font options don't have keyboard accelerators.491goog.dom.classlist.add(492goog.asserts.assert(button.getMenu().getContentElement()),493goog.getCssName('goog-menu-noaccel'));494495// How to update this button's state.496button.updateFromValue = function(value) {497'use strict';498// Normalize value to null or a non-empty string (sometimes we get499// the empty string, sometimes we get false...), extract the substring500// up to the first comma to get the primary font name, and normalize501// to lowercase. This allows us to map a font spec like "Arial,502// Helvetica, sans-serif" to a font menu item.503// TODO (attila): Try to make this more robust.504let item = null;505if (value && value.length > 0) {506item = /** @type {goog.ui.MenuItem} */ (button.getMenu().getChild(507goog.ui.editor.ToolbarFactory.getPrimaryFont(value)));508}509const selectedItem = button.getSelectedItem();510if (item != selectedItem) {511button.setSelectedItem(item);512}513};514return button;515};516517518/**519* Creates a toolbar button with the given ID, tooltip, and caption. Applies520* any custom CSS class names to the button's caption element. Use to create a521* font size button, filled with default font sizes.522* @param {string} id Button ID; must equal a {@link goog.editor.Command} for523* built-in buttons, anything else for custom buttons.524* @param {string} tooltip Tooltip to be shown on hover.525* @param {goog.ui.ControlContent} caption Button caption.526* @param {string=} opt_classNames CSS class name(s) to apply to the caption527* element.528* @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer; defaults529* to {@link goog.ui.ToolbarMebuButtonRenderer} if unspecified.530* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM531* creation; defaults to the current document if unspecified.532* @return {!goog.ui.Button} A toolbar button.533* @private534* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration535*/536goog.ui.editor.DefaultToolbar.fontSizeFactory_ = function(537id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {538'use strict';539const button = goog.ui.editor.ToolbarFactory.makeSelectButton(540id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);541goog.ui.editor.DefaultToolbar.addDefaultFontSizes(button);542button.setDefaultCaption(goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_NORMAL);543// Font size options don't have keyboard accelerators.544goog.dom.classlist.add(545goog.asserts.assert(button.getMenu().getContentElement()),546goog.getCssName('goog-menu-noaccel'));547// How to update this button's state.548button.updateFromValue = function(value) {549'use strict';550// Webkit pre-534.7 returns a string like '32px' instead of the equivalent551// integer, so normalize that first.552// NOTE(user): Gecko returns "6" so can't just normalize all553// strings, only ones ending in "px".554if (typeof value === 'string' && goog.style.getLengthUnits(value) == 'px') {555value = goog.ui.editor.ToolbarFactory.getLegacySizeFromPx(556parseInt(value, 10));557}558// Normalize value to null or a positive integer (sometimes we get559// the empty string, sometimes we get false, or -1 if the above560// normalization didn't match to a particular 0-7 size)561value = value > 0 ? value : null;562if (value != button.getValue()) {563button.setValue(value);564}565};566return button;567};568569570/**571* Function to update the state of a color menu button.572* @param {goog.ui.ToolbarColorMenuButton} button The button to which the573* color menu is attached.574* @param {number} color Color value to update to.575* @private576*/577goog.ui.editor.DefaultToolbar.colorUpdateFromValue_ = function(button, color) {578'use strict';579let value = color;580581try {582if (goog.userAgent.IE) {583// IE returns a number that, converted to hex, is a BGR color.584// Convert from decimal to BGR to RGB.585const hex = '000000' + value.toString(16);586const bgr = hex.slice(-6);587value =588'#' + bgr.substring(4, 6) + bgr.substring(2, 4) + bgr.substring(0, 2);589}590if (value != button.getValue()) {591button.setValue(/** @type {string} */ (value));592}593} catch (ex) {594// TODO(attila): Find out when/why this happens.595}596};597598599/**600* Creates a toolbar button with the given ID, tooltip, and caption. Applies601* any custom CSS class names to the button's caption element. Use to create602* a font color button.603* @param {string} id Button ID; must equal a {@link goog.editor.Command} for604* built-in buttons, anything else for custom buttons.605* @param {string} tooltip Tooltip to be shown on hover.606* @param {goog.ui.ControlContent} caption Button caption.607* @param {string=} opt_classNames CSS class name(s) to apply to the caption608* element.609* @param {goog.ui.ColorMenuButtonRenderer=} opt_renderer Button renderer;610* defaults to {@link goog.ui.ToolbarColorMenuButtonRenderer} if611* unspecified.612* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM613* creation; defaults to the current document if unspecified.614* @return {!goog.ui.Button} A toolbar button.615* @private616* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration617*/618goog.ui.editor.DefaultToolbar.fontColorFactory_ = function(619id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {620'use strict';621const button = goog.ui.editor.ToolbarFactory.makeColorMenuButton(622id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);623// Initialize default foreground color.624button.setSelectedColor('#000');625button.updateFromValue = goog.partial(626goog.ui.editor.DefaultToolbar.colorUpdateFromValue_,627/** @type {!goog.ui.ToolbarColorMenuButton} */ (button));628return button;629};630631632/**633* Creates a toolbar button with the given ID, tooltip, and caption. Applies634* any custom CSS class names to the button's caption element. Use to create635* a font background color button.636* @param {string} id Button ID; must equal a {@link goog.editor.Command} for637* built-in buttons, anything else for custom buttons.638* @param {string} tooltip Tooltip to be shown on hover.639* @param {goog.ui.ControlContent} caption Button caption.640* @param {string=} opt_classNames CSS class name(s) to apply to the caption641* element.642* @param {goog.ui.ColorMenuButtonRenderer=} opt_renderer Button renderer;643* defaults to {@link goog.ui.ToolbarColorMenuButtonRenderer} if644* unspecified.645* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM646* creation; defaults to the current document if unspecified.647* @return {!goog.ui.Button} A toolbar button.648* @private649* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration650*/651goog.ui.editor.DefaultToolbar.backgroundColorFactory_ = function(652id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {653'use strict';654const button = goog.ui.editor.ToolbarFactory.makeColorMenuButton(655id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);656// Initialize default background color.657button.setSelectedColor('#FFF');658button.updateFromValue = goog.partial(659goog.ui.editor.DefaultToolbar.colorUpdateFromValue_,660/** @type {!goog.ui.ToolbarColorMenuButton} */ (button));661return button;662};663664665/**666* Creates a toolbar button with the given ID, tooltip, and caption. Applies667* any custom CSS class names to the button's caption element. Use to create668* the format menu, prefilled with default formats.669* @param {string} id Button ID; must equal a {@link goog.editor.Command} for670* built-in buttons, anything else for custom buttons.671* @param {string} tooltip Tooltip to be shown on hover.672* @param {goog.ui.ControlContent} caption Button caption.673* @param {string=} opt_classNames CSS class name(s) to apply to the caption674* element.675* @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer;676* defaults to677* {@link goog.ui.ToolbarMenuButtonRenderer} if unspecified.678* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM679* creation; defaults to the current document if unspecified.680* @return {!goog.ui.Button} A toolbar button.681* @private682* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration683*/684goog.ui.editor.DefaultToolbar.formatBlockFactory_ = function(685id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {686'use strict';687const button = goog.ui.editor.ToolbarFactory.makeSelectButton(688id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);689goog.ui.editor.DefaultToolbar.addDefaultFormatOptions(button);690button.setDefaultCaption(goog.ui.editor.DefaultToolbar.MSG_FORMAT_NORMAL);691// Format options don't have keyboard accelerators.692goog.dom.classlist.add(693goog.asserts.assert(button.getMenu().getContentElement()),694goog.getCssName('goog-menu-noaccel'));695// How to update this button.696button.updateFromValue = function(value) {697'use strict';698// Normalize value to null or a nonempty string (sometimes we get699// the empty string, sometimes we get false...)700value = value && value.length > 0 ? value : null;701if (value != button.getValue()) {702button.setValue(value);703}704};705return button;706};707708709// Messages used for tooltips and captions.710711712/** @desc Format menu tooltip. */713goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_TITLE = goog.getMsg('Format');714715716/** @desc Format menu caption. */717goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_CAPTION = goog.getMsg('Format');718719720/** @desc Undo button tooltip. */721goog.ui.editor.DefaultToolbar.MSG_UNDO_TITLE = goog.getMsg('Undo');722723724/** @desc Redo button tooltip. */725goog.ui.editor.DefaultToolbar.MSG_REDO_TITLE = goog.getMsg('Redo');726727728/** @desc Font menu tooltip. */729goog.ui.editor.DefaultToolbar.MSG_FONT_FACE_TITLE = goog.getMsg('Font');730731732/** @desc Font size menu tooltip. */733goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_TITLE = goog.getMsg('Font size');734735736/** @desc Text foreground color menu tooltip. */737goog.ui.editor.DefaultToolbar.MSG_FONT_COLOR_TITLE = goog.getMsg('Text color');738739740/** @desc Bold button tooltip. */741goog.ui.editor.DefaultToolbar.MSG_BOLD_TITLE = goog.getMsg('Bold');742743744/** @desc Italic button tooltip. */745goog.ui.editor.DefaultToolbar.MSG_ITALIC_TITLE = goog.getMsg('Italic');746747748/** @desc Underline button tooltip. */749goog.ui.editor.DefaultToolbar.MSG_UNDERLINE_TITLE = goog.getMsg('Underline');750751752/** @desc Text background color menu tooltip. */753goog.ui.editor.DefaultToolbar.MSG_BACKGROUND_COLOR_TITLE =754goog.getMsg('Text background color');755756757/** @desc Link button tooltip. */758goog.ui.editor.DefaultToolbar.MSG_LINK_TITLE =759goog.getMsg('Add or remove link');760761762/** @desc Numbered list button tooltip. */763goog.ui.editor.DefaultToolbar.MSG_ORDERED_LIST_TITLE =764goog.getMsg('Numbered list');765766767/** @desc Bullet list button tooltip. */768goog.ui.editor.DefaultToolbar.MSG_UNORDERED_LIST_TITLE =769goog.getMsg('Bullet list');770771772/** @desc Outdent button tooltip. */773goog.ui.editor.DefaultToolbar.MSG_OUTDENT_TITLE =774goog.getMsg('Decrease indent');775776777/** @desc Indent button tooltip. */778goog.ui.editor.DefaultToolbar.MSG_INDENT_TITLE = goog.getMsg('Increase indent');779780781/** @desc Align left button tooltip. */782goog.ui.editor.DefaultToolbar.MSG_ALIGN_LEFT_TITLE = goog.getMsg('Align left');783784785/** @desc Align center button tooltip. */786goog.ui.editor.DefaultToolbar.MSG_ALIGN_CENTER_TITLE =787goog.getMsg('Align center');788789790/** @desc Align right button tooltip. */791goog.ui.editor.DefaultToolbar.MSG_ALIGN_RIGHT_TITLE =792goog.getMsg('Align right');793794795/** @desc Justify button tooltip. */796goog.ui.editor.DefaultToolbar.MSG_JUSTIFY_TITLE = goog.getMsg('Justify');797798799/** @desc Remove formatting button tooltip. */800goog.ui.editor.DefaultToolbar.MSG_REMOVE_FORMAT_TITLE =801goog.getMsg('Remove formatting');802803804/** @desc Insert image button tooltip. */805goog.ui.editor.DefaultToolbar.MSG_IMAGE_TITLE = goog.getMsg('Insert image');806807808/** @desc Strike through button tooltip. */809goog.ui.editor.DefaultToolbar.MSG_STRIKE_THROUGH_TITLE =810goog.getMsg('Strikethrough');811812813/** @desc Left-to-right button tooltip. */814goog.ui.editor.DefaultToolbar.MSG_DIR_LTR_TITLE = goog.getMsg('Left-to-right');815816817/** @desc Right-to-left button tooltip. */818goog.ui.editor.DefaultToolbar.MSG_DIR_RTL_TITLE = goog.getMsg('Right-to-left');819820821/** @desc Blockquote button tooltip. */822goog.ui.editor.DefaultToolbar.MSG_BLOCKQUOTE_TITLE = goog.getMsg('Quote');823824825/** @desc Edit HTML button tooltip. */826goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_TITLE =827goog.getMsg('Edit HTML source');828829830/** @desc Subscript button tooltip. */831goog.ui.editor.DefaultToolbar.MSG_SUBSCRIPT = goog.getMsg('Subscript');832833834/** @desc Superscript button tooltip. */835goog.ui.editor.DefaultToolbar.MSG_SUPERSCRIPT = goog.getMsg('Superscript');836837838/** @desc Edit HTML button caption. */839goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_CAPTION = goog.getMsg('Edit HTML');840841842/**843* Map of `goog.editor.Command`s to toolbar button descriptor objects,844* each of which has the following attributes:845* <ul>846* <li>`command` - The command corresponding to the847* button (mandatory)848* <li>`tooltip` - Tooltip text (optional); if unspecified, the button849* has no hover text850* <li>`caption` - Caption to display on the button (optional); if851* unspecified, the button has no text caption852* <li>`classes` - CSS class name(s) to be applied to the button's853* element when rendered (optional); if unspecified, defaults to854* 'tr-icon'855* plus 'tr-' followed by the command ID, but without any leading '+'856* character (e.g. if the command ID is '+undo', then `classes`857* defaults to 'tr-icon tr-undo')858* <li>`factory` - factory function used to create the button, which859* must accept `id`, `tooltip`, `caption`, and860* `classes` as arguments, and must return an instance of861* {@link goog.ui.Button} or an appropriate subclass (optional); if862* unspecified, defaults to863* {@link goog.ui.editor.DefaultToolbar.makeToggleButton},864* since most built-in toolbar buttons are toggle buttons865* <li>(@code queryable} - Whether the button's state should be queried866* when updating the toolbar (optional).867* </ul>868* Note that this object is only used for creating toolbar buttons for869* built-in editor commands; custom buttons aren't listed here. Please don't870* try to hack this!871* @private {!Object<string, !goog.ui.editor.ButtonDescriptor>}.872*/873goog.ui.editor.DefaultToolbar.buttons_ = {};874875876/**877* @typedef {{878* command: string,879* tooltip: (undefined|string),880* caption: (undefined|goog.ui.ControlContent),881* classes: (undefined|string),882* factory: (undefined|!Function),883* queryable:(undefined|boolean)}}884*/885goog.ui.editor.ButtonDescriptor;886887888/**889* Built-in toolbar button descriptors. See890* {@link goog.ui.editor.DefaultToolbar.buttons_} for details on button891* descriptor objects. This array is processed at JS parse time; each item is892* inserted into {@link goog.ui.editor.DefaultToolbar.buttons_}, and the array893* itself is deleted and (hopefully) garbage-collected.894* @private {Array<!goog.ui.editor.ButtonDescriptor>}895*/896goog.ui.editor.DefaultToolbar.button_list_ = [897{898command: goog.editor.Command.UNDO,899tooltip: goog.ui.editor.DefaultToolbar.MSG_UNDO_TITLE,900classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-undo'),901factory: goog.ui.editor.DefaultToolbar.undoRedoButtonFactory_,902queryable: true903},904{905command: goog.editor.Command.REDO,906tooltip: goog.ui.editor.DefaultToolbar.MSG_REDO_TITLE,907classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-redo'),908factory: goog.ui.editor.DefaultToolbar.undoRedoButtonFactory_,909queryable: true910},911{912command: goog.editor.Command.FONT_FACE,913tooltip: goog.ui.editor.DefaultToolbar.MSG_FONT_FACE_TITLE,914classes: goog.getCssName('tr-fontName'),915factory: goog.ui.editor.DefaultToolbar.fontFaceFactory_,916queryable: true917},918{919command: goog.editor.Command.FONT_SIZE,920tooltip: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_TITLE,921classes: goog.getCssName('tr-fontSize'),922factory: goog.ui.editor.DefaultToolbar.fontSizeFactory_,923queryable: true924},925{926command: goog.editor.Command.BOLD,927tooltip: goog.ui.editor.DefaultToolbar.MSG_BOLD_TITLE,928classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-bold'),929queryable: true930},931{932command: goog.editor.Command.ITALIC,933tooltip: goog.ui.editor.DefaultToolbar.MSG_ITALIC_TITLE,934classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-italic'),935queryable: true936},937{938command: goog.editor.Command.UNDERLINE,939tooltip: goog.ui.editor.DefaultToolbar.MSG_UNDERLINE_TITLE,940classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-underline'),941queryable: true942},943{944command: goog.editor.Command.FONT_COLOR,945tooltip: goog.ui.editor.DefaultToolbar.MSG_FONT_COLOR_TITLE,946classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-foreColor'),947factory: goog.ui.editor.DefaultToolbar.fontColorFactory_,948queryable: true949},950{951command: goog.editor.Command.BACKGROUND_COLOR,952tooltip: goog.ui.editor.DefaultToolbar.MSG_BACKGROUND_COLOR_TITLE,953classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-backColor'),954factory: goog.ui.editor.DefaultToolbar.backgroundColorFactory_,955queryable: true956},957{958command: goog.editor.Command.LINK,959tooltip: goog.ui.editor.DefaultToolbar.MSG_LINK_TITLE,960caption: goog.ui.editor.messages.MSG_LINK_CAPTION,961classes: goog.getCssName('tr-link'),962queryable: true963},964{965command: goog.editor.Command.ORDERED_LIST,966tooltip: goog.ui.editor.DefaultToolbar.MSG_ORDERED_LIST_TITLE,967classes: goog.getCssName('tr-icon') + ' ' +968goog.getCssName('tr-insertOrderedList'),969queryable: true970},971{972command: goog.editor.Command.UNORDERED_LIST,973tooltip: goog.ui.editor.DefaultToolbar.MSG_UNORDERED_LIST_TITLE,974classes: goog.getCssName('tr-icon') + ' ' +975goog.getCssName('tr-insertUnorderedList'),976queryable: true977},978{979command: goog.editor.Command.OUTDENT,980tooltip: goog.ui.editor.DefaultToolbar.MSG_OUTDENT_TITLE,981classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-outdent'),982factory: goog.ui.editor.ToolbarFactory.makeButton983},984{985command: goog.editor.Command.INDENT,986tooltip: goog.ui.editor.DefaultToolbar.MSG_INDENT_TITLE,987classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-indent'),988factory: goog.ui.editor.ToolbarFactory.makeButton989},990{991command: goog.editor.Command.JUSTIFY_LEFT,992tooltip: goog.ui.editor.DefaultToolbar.MSG_ALIGN_LEFT_TITLE,993classes:994goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyLeft'),995queryable: true996},997{998command: goog.editor.Command.JUSTIFY_CENTER,999tooltip: goog.ui.editor.DefaultToolbar.MSG_ALIGN_CENTER_TITLE,1000classes:1001goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyCenter'),1002queryable: true1003},1004{1005command: goog.editor.Command.JUSTIFY_RIGHT,1006tooltip: goog.ui.editor.DefaultToolbar.MSG_ALIGN_RIGHT_TITLE,1007classes:1008goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyRight'),1009queryable: true1010},1011{1012command: goog.editor.Command.JUSTIFY_FULL,1013tooltip: goog.ui.editor.DefaultToolbar.MSG_JUSTIFY_TITLE,1014classes:1015goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyFull'),1016queryable: true1017},1018{1019command: goog.editor.Command.REMOVE_FORMAT,1020tooltip: goog.ui.editor.DefaultToolbar.MSG_REMOVE_FORMAT_TITLE,1021classes:1022goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-removeFormat'),1023factory: goog.ui.editor.ToolbarFactory.makeButton1024},1025{1026command: goog.editor.Command.IMAGE,1027tooltip: goog.ui.editor.DefaultToolbar.MSG_IMAGE_TITLE,1028classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-image'),1029factory: goog.ui.editor.ToolbarFactory.makeButton1030},1031{1032command: goog.editor.Command.STRIKE_THROUGH,1033tooltip: goog.ui.editor.DefaultToolbar.MSG_STRIKE_THROUGH_TITLE,1034classes:1035goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-strikeThrough'),1036queryable: true1037},1038{1039command: goog.editor.Command.SUBSCRIPT,1040tooltip: goog.ui.editor.DefaultToolbar.MSG_SUBSCRIPT,1041classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-subscript'),1042queryable: true1043},1044{1045command: goog.editor.Command.SUPERSCRIPT,1046tooltip: goog.ui.editor.DefaultToolbar.MSG_SUPERSCRIPT,1047classes:1048goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-superscript'),1049queryable: true1050},1051{1052command: goog.editor.Command.DIR_LTR,1053tooltip: goog.ui.editor.DefaultToolbar.MSG_DIR_LTR_TITLE,1054classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-ltr'),1055queryable: true1056},1057{1058command: goog.editor.Command.DIR_RTL,1059tooltip: goog.ui.editor.DefaultToolbar.MSG_DIR_RTL_TITLE,1060classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-rtl'),1061factory: goog.ui.editor.DefaultToolbar.rtlButtonFactory_,1062queryable: true1063},1064{1065command: goog.editor.Command.BLOCKQUOTE,1066tooltip: goog.ui.editor.DefaultToolbar.MSG_BLOCKQUOTE_TITLE,1067classes:1068goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-BLOCKQUOTE'),1069queryable: true1070},1071{1072command: goog.editor.Command.FORMAT_BLOCK,1073tooltip: goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_TITLE,1074caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_CAPTION,1075classes: goog.getCssName('tr-formatBlock'),1076factory: goog.ui.editor.DefaultToolbar.formatBlockFactory_,1077queryable: true1078},1079{1080command: goog.editor.Command.EDIT_HTML,1081tooltip: goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_TITLE,1082caption: goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_CAPTION,1083classes: goog.getCssName('tr-editHtml'),1084factory: goog.ui.editor.ToolbarFactory.makeButton1085}1086];108710881089(function() {1090'use strict';1091// Create the goog.ui.editor.DefaultToolbar.buttons_ map from1092// goog.ui.editor.DefaultToolbar.button_list_.1093for (let i = 0, button; button = goog.ui.editor.DefaultToolbar.button_list_[i];1094i++) {1095goog.ui.editor.DefaultToolbar.buttons_[button.command] = button;1096}10971098// goog.ui.editor.DefaultToolbar.button_list_ is no longer needed1099// once the map is ready.1100goog.ui.editor.DefaultToolbar.button_list_ = null;1101})();110211031104