- 快速入门
- Explore advanced topics
- Accessibility
- Editor control identifiers
- Annotations
- Editor events
- tinymce-AddOnManager
- Boilerplate content CSS
- Handle asynchronous image uploads
- tinymce-Annotator
- tinymce-FocusEvent
- Configuring callbacks for Comments 2-0
- Keyboard shortcuts
- tinymce-CommandEvent
- tinymce-FocusManager
- tinymce-WindowManager
- Toolbar buttons
- PHP image upload handler
- tinymce-ContentEvent
- tinymce-Formatter
- tinymce-dom-DomQuery
- Create a plugin for TinyMCE
- Security
- tinymce-Editor
- tinymce-NotificationManager
- tinymce-dom-DOMUtils
- Create a skin for TinyMCE
- Usage with module loaders
- tinymce-EditorCommands
- tinymce-Plugin
- tinymce-dom-ScriptLoader
- Create custom notifications
- TinyMCE plugin Yeoman generator
- tinymce-EditorManager
- tinymce-ProgressStateEvent
- tinymce-dom-BookmarkManager
- Editor command identifiers
- tinymce-Formatter
- tinymce-EditorObservable
- tinymce-ResizeEvent
- tinymce-dom-ControlSelection
- tinymce
- tinymce-Env
- tinymce-Shortcuts
- tinymce-dom-DomQuery
- tinymce-Event
- tinymce-Theme
- tinymce-dom-DOMUtils
- tinymce-UndoManager
- tinymce-dom-EventUtils
- tinymce-dom-RangeUtils
- tinymce-dom-ScriptLoader
- tinymce-dom-Selection
- tinymce-html-Schema
- tinymce-dom-Serializer
- tinymce-html-Serializer
- tinymce-util-I18n
- tinymce-dom-TreeWalker
- tinymce-html-Styles
- tinymce-util-JSON
- Cloud deployment of plugins Only
- tinymce-editor-ui-registry
- tinymce-html-Writer
- tinymce-util-JSONRequest
- Version compatibility reference
- Image & file upload options
- tinymce-geom-Rect
- tinymce-util-LocalStorage
- Configuration options reference
- Integration and setup options
- tinymce-html-DomParser
- tinymce-util-Observable
- tinymce-util-Observable
- Advanced editing behaviors
- JWT authentication setup
- tinymce-html-Entities
- tinymce-util-URI
- tinymce-util-Tools
- Content appearance options
- Localization options
- tinymce-html-Node
- tinymce-util-Color
- tinymce-util-XHR
- Content filtering options
- Spelling options
- tinymce-html-SaxParser
- tinymce-util-Delay
- Cloud deployment guide
- Content formatting options
- URL handling options
- tinymce-util-EventDispatcher
- Cloud deployment of editor & plugins
- Contribute to documentation
- Examples & demos
- Specify editor & plugin versions
- Contribute to TinyMCE development
- Basic example
- User interface options
- Case Change
- Checklist plugin
- Classic editor example
- Comments 2-0 Demo
- Custom formats example
- Custom menu item
- HTML5 formats example
- Valid elements example
- Custom toolbar button
- Format Painter
- Live example
- Embed rich media
- Full featured example
- Premium features
- Configure Enhanced Media Embed Server
- Server-side component installation
- Custom toolbar menu button
- Image tools example
- Accessibility checking
- Integrate Enhanced Media Embed Server
- Configure server-side components
- Custom toolbar split button
- Inline editor example
- Advanced source code editing
- Format Painter
- Troubleshoot server-side components
- Tiny Drive
- Local upload
- Case Change
- Get TinyMCE bugs fixed
- Professional support
- Distraction-free editor example
- Page Embed plugin
- Checking links as-you-type
- Mentions
- System requirements
- Basic local file picker
- Permanent Pen plugin
- Check Spelling As-You-Type
- Self-hosted file management
- Commenting & collaboration
- URL conversion example
- Adding custom dictionaries
- Page Embed plugin
- Cloud-based file management
- Checklist plugin
- Paste from Word
- Introduction & getting started
- Permanent Pen Plugin
- Advanced installation choices
- Logo & attribution requirements
- Basic setup
- Customizing the editor UI
- TinyMCE distraction-free editing mode
- Filtering TinyMCE content
- Setup inline editing mode
- Rails integration
- Get Support & FAQ
- Work with plugins to extend TinyMCE
- React integration
- Advanced Code Editor plugin
- Localize TinyMCE
- Integrate with other projects
- TinyMCE for Swing integration
- Advanced List plugin
- Code plugin
- Use multiple TinyMCE instances in a single page
- Angular 2+ integration
- Vue integration
- Anchor plugin
- Code Sample plugin
- Check spelling in TinyMCE
- Angular 1 integration
- WordPress integration
- Autolink plugin
- Color Picker plugin
- System requirements
- Bootstrap integration
- Migrating from TinyMCE 4-x to TinyMCE 5-0-
- Autoresize plugin
- Comments 2-0
- Uploading images and files
- Dojo integration
- TinyMCE Mobile
- Autosave plugin
- TinyMCE classic editing mode
- jQuery integration
- BBCode plugin
- Context Menu plugin
- KnockoutJS integration
- Add plugins to TinyMCE
- Case Change
- Directionality plugin
- Accessibility Checker plugin
- Character Map plugin
- Drive plugin
- Checklist plugin
- Emoticons plugin
- Format Painter
- Full Page plugin
- Full Screen plugin
- Link Checker plugin
- Help plugin
- Lists plugin
- Paste plugin
- Horizontal Rule plugin
- Media plugin
- Permanent Pen Plugin
- Table plugin
- Image plugin
- Enhanced Media Embed plugin
- PowerPaste plugin
- Template plugin
- User interface components
- Image Tools plugin
- Mentions plugin
- Preview plugin
- Text Color plugin
- Autocompleter
- Import CSS plugin
- MoxieManager plugin
- Print plugin
- Text Pattern plugin
- Context forms
- Insert Date/Time plugin
- Nonbreaking Space plugin
- Quick Toolbar plugin
- Spell Checker Pro plugin
- Context menu
- Legacy Output plugin
- Noneditable plugin
- Save plugin
- Table of Contents plugin
- Context toolbar
- Link plugin
- Page Break plugin
- Search and Replace plugin
- Visual Blocks plugin
- Custom sidebar
- Page Embed plugin
- Spell Checker plugin
- Visual Characters plugin
- Dialog
- Tab Focus plugin
- Word Count plugin
- Dialog components
- Quick start
- Custom menu items
- Toolbar buttons
- Types of toolbar buttons
Content formatting options
formats
This option is used to override default TinyMCE formats or add custom formats to the editor.
TinyMCE is equipped with a formatting engine that allows you to register a set of styles and attributes as a named format. For example, the bold
format is the style that is applied to text when the bold button is clicked.
Check out the custom formats example for a demonstration of this option.
The formats
configuration option takes an object of format name and format specification pairs. Each format specification must be defined as an object where the value is either an object of format options or an array of such objects.
If a format is specified with an array of variants, when the format is applied only the first item in the array will be used. However, when trying to match the format to remove or update the UI, the other variants in the array will be considered as well. So the first format is a kind of primary format, and the rest are secondary formats.
The following is an example of an array of format specification that contains three variants:
bold: [
{ inline: 'strong', remove: 'all' },
{ inline: 'span', styles: { fontWeight: 'bold' } },
{ inline: 'b', remove: 'all' }
],
Style merging
Similar elements and styles are merged by default to reduce the output HTML size. So for example, if a font size and font face are selected for a word, it merges these two styles into one span
element instead of one span
for each format type.
Built-in formats
TinyMCE has some built in formats that can be overridden. These are:
- alignleft
- aligncenter
- alignright
- alignjustify
- bold
- italic
- underline
- strikethrough
- forecolor
- hilitecolor
- fontname
- fontsize
- blockquote
- removeformat
- p
- h1, h2, h3, h4, h5, h6
- div
- address
- pre
- div
- code
- dt, dd
- samp
Some built-in formats fontsize
, fontname
, forecolor
, hilitecolor
use a variable in their definition named %value
. This gets replaced with the user selected item such as a color
value. Check the variable substitution section below for details.
Format Type
There are three types of formats:
- Block format
- Inline format
- Selector format
All three format types can be used with the formats
configuration option. They can also be used to specify a new format item in the style_formats
configuration option. However, a format that is specified using formats
is then registered with the editor, and can be referred to by name in style_formats
rather than needing to specify the format again.
For example, these two configurations are equivalent:
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for h1 to have a class of heading
h1: { block: 'h1', classes: 'heading' }
},
style_formats: [
// Adds the h1 format defined above to style_formats
{ title: 'My heading', format: 'h1' }
]
});
tinymce.init({
selector: 'textarea',
style_formats: [
// Adds a h1 format to style_formats that applies a class of heading
{ title: 'My heading', block: 'h1', classes: 'heading' }
]
});
A registered format can also be used by name with the built-in formatter methods. See /configure/content-formatting/#usingcustomformats for an example.
block
Tag name of the block element to use as a wrapper, for example, h1
. Existing block elements within the selection are replaced with this block element.
Type: String
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for h1 to have a class of heading
h1: { block: 'h1', classes: 'heading' }
}
});
inline
Tag name of the inline element to use as a wrapper, for example, span
is used to wrap the current selection.
Type: String
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for the bold button to produce a span with a bold class
bold: { inline: 'span', classes: 'bold' }
}
});
selector
CSS3 selector pattern that is used to find elements within the selection. It can be used to apply classes to specific elements only, for example only to odd rows in a table.
Type: String
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the alignment buttons to add a class to each of the matching selector elements
alignleft: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'left' },
aligncenter: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'center' },
alignright: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'right' },
alignjustify: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'full' }
}
});
Format parameters
For each format some additional parameters can be specified:
classes
Space-separated list of classes that are applied to the selected or newly created inline/block element.
Type: String
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for the bold button to produce a span with a bold class
bold: { inline: 'span', classes: 'bold' }
}
});
styles
Key/value object with CSS styles to apply to the selected or newly created inline/block element (e.g. color
, backgroundColor
, textDecoration
, etc).
Type: Object
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for the bold button to produce a span with style with font-width: bold
bold: { inline: 'span', styles: { 'font-weight': 'bold' } }
}
});
attributes
Key/value object with attributes to apply to the selected or newly created inline/block element.
Type: Object
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for the bold button to produce a strong with data-style attribute
bold: { inline: 'strong', attributes: { 'data-style': 'bold' } }
}
});
exact
Makes sure that the format is not merged with other wrappers having the same format. We use it to avoid conflicts between text-decorations for underline
and strikethrough
formats.
Type: Boolean
Default: false
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for the underline button to produce a span with a class and not merge that underline into parent spans
underline: { inline: 'span', styles: { 'text-decoration': 'underline' }, exact: true },
strikethrough: { inline: 'span', styles: { 'text-decoration': 'line-through' }, exact: true }
}
});
wrapper
States that the format is a container format for block elements. For example, a div
wrapper or blockquote
.
Type: Boolean
Default: false
Example
tinymce.init({
selector: 'textarea',
formats: {
// A custom format that wraps blocks into a div with the specified wrapper class
'custom-wrapper': { block: 'div', classes: 'wrapper', wrapper: true }
}
});
remove
Specifies what the remove behavior of the element should be when the format is removed.
Type: String
Default: none
for Selector
formats and empty
for all other format types.
This can be set to three different modes:
- none: Only styles, classes or attributes are removed from the element the element is never removed.
- empty: If the element has no styles, classes, or attributes then the element is removed.
- all: Removes the element regardless of its styles, classes, and or attributes.
Example
tinymce.init({
selector: 'textarea',
extended_valid_elements: 'span[*]', // Needed to retain spans without attributes these are removed by default
formats: {
removeformat: [
// Configures `clear formatting` to remove specified elements regardless of it's attributes
{ selector: 'b,strong,em,i,font,u,strike', remove: 'all' },
// Configures `clear formatting` to remove the class red from spans and if the element then becomes empty i.e has no attributes it gets removed
{ selector: 'span', classes: 'red', remove: 'empty' },
// Configures `clear formatting` to remove the class green from spans and if the element then becomes empty it's left intact
{ selector: 'span', classes: 'green', remove: 'none' }
]
}
});
block_expand
This option controls if the selection should expand upwards to the closest matching block element. This can be useful when configuring removeformat
to remove block elements. So if the selection start is at the beginning of a matching block, then that matching block will be included as well. If the end of the selection is at the end of a matching block element then that parent element will be included as well.
So if the selection is from a to b in this html contents <h1><b>[a</b></h1><p>b]</p>
then the h1 will be removed even if it’s not part of the actual selection.
Type: Boolean
Example
tinymce.init({
selector: 'textarea',
formats: {
removeformat: [
{
selector: 'h1,h2,h3,h4,h5,h6',
remove: 'all',
split: false,
expand: false,
block_expand: true,
deep: true
},
{
selector: 'a,b,strong,em,i,font,u,strike,sub,sup,dfn,code,samp,kbd,var,cite,mark,q,del,ins',
remove: 'all',
split: true,
expand: false,
deep: true
},
{ selector: 'span', attributes: ['style', 'class'], remove: 'empty', split: true, expand: false, deep: true },
{ selector: '*', attributes: ['style', 'class'], split: false, expand: false, deep: true }
]
}
});
deep
Enables control for removing the child elements of the matching format. This is set to false
by default on selector formats. As a result, when a class is removed from a selected table class, disabling deep
retains the class in the child elements within the other nested tables.
Type: Boolean
Default: false
for selector
formats
Example
tinymce.init({
selector: 'textarea',
formats: {
// A custom format that wraps blocks into a div with the specified wrapper class
'custom-deep': { inline: 'span', classes: 'myclass', deep: false }
}
});
Example of usage of the formats option
This example overrides some of the built-in formats and tells TinyMCE to apply classes instead of inline styles. It also includes a custom format that produced h1
elements with a title attribute and a red
CSS style.
Type: Object
Example
// Output elements in HTML style
tinymce.init({
selector: 'textarea', // change this value according to your html
formats: {
alignleft: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'left' },
aligncenter: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'center' },
alignright: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'right' },
alignjustify: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'full' },
bold: { inline: 'span', classes: 'bold' },
italic: { inline: 'span', classes: 'italic' },
underline: { inline: 'span', classes: 'underline', exact: true },
strikethrough: { inline: 'del' },
forecolor: { inline: 'span', classes: 'forecolor', styles: { color: '%value' } },
hilitecolor: { inline: 'span', classes: 'hilitecolor', styles: { backgroundColor: '%value' } },
custom_format: { block: 'h1', attributes: { title: 'Header' }, styles: { color: 'red' } }
}
});
Using custom formats
Custom formats can be handled through the TinyMCE API. Here is a basic example of usage for the custom format defined above.
// Applying the specified format
tinymce.activeEditor.formatter.apply('custom_format');
// Removing the specified format
tinymce.activeEditor.formatter.remove('custom_format');
Variable substitution
Variables can be used in the format definition. These variables are then replaced with the ones specified in the call to the apply function. Here is an example of how to use variables within formats.
// Registering the special format with a variable
tinymce.activeEditor.formatter.register('custom_format', { inline: 'span', styles: { color: '%value' } });
// Applying the specified format with the variable specified
tinymce.activeEditor.formatter.apply('custom_format', { value: 'red' });
Removing a format
Use the removeformat
option to specify how the clear formatting
feature should work in the editor.
Type: Array
Example
tinymce.init({
selector: 'textarea', // change this value according to your HTML
formats: {
removeformat: [
{
selector: 'b,strong,em,i,font,u,strike,sub,sup,dfn,code,samp,kbd,var,cite,mark,q,del,ins',
remove: 'all',
split: true,
block_expand: true,
expand: false,
deep: true
},
{ selector: 'span', attributes: ['style', 'class'], remove: 'empty', split: true, expand: false, deep: true },
{ selector: '*', attributes: ['style', 'class'], split: false, expand: false, deep: true }
]
}
});
indentation
The indentation option allows specification of the indentation level for indent/outdent buttons in the UI.
The indentation option defaults to 30px but can be any value.
Type: String
Default Value: 30px
Example
tinymce.init({
selector: 'textarea', // change this value according to your HTML
indentation : '20pt'
});
indent_use_margin
The indent_use_margin option is set if the editor should use margin instead of padding when indenting content.
Type: Boolean
Default Value: false
Example
tinymce.init({
selector: 'textarea', // change this value according to your HTML
indentation : '20pt',
indent_use_margin: true
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论