首页
话题
消息
手册
我的
返回介绍

Context menu

发布于 2019-05-06 字数20403 浏览 783 评论 0

The context menu is a configurable component that appears when the user right clicks in the editable area. By default it does not disable the operating system’s native context menu, if there are no items to display at the cursor position the native context menu will be shown.

The context menu supports both individual menu items and dynamic context menu sections.

Live example

TinyMCE HTML JS Edit on CodePen


<textarea id="context-menu">
  <p style="text-align: center;">
    <img title="TinyMCE Logo" src="//www.tiny.cloud/images/glyph-tinymce@2x.png" alt="TinyMCE Logo" width="110" height="97" />
  </p>

  <h2 style="text-align: center;">Welcome to the TinyMCE editor demo!</h2>

  <p>
    Please try out the features provided in this basic example.<br>
    Note that any <strong>MoxieManager</strong> file and image management functionality in this example is part of our commercial offering – the demo is to show the integration.
  </p>

  <h2>Got questions or need help?</h2>

  <ul>
    <li>Our <a external-link="true" href="https://www.tiny.cloud/docs/">documentation</a> is a great resource for learning how to configure TinyMCE.</li>
    <li>Have a specific question? Visit the <a external-link="true" href="https://community.tinymce.com/forum/" target="_blank">Community Forum</a>.</li>
    <li>We also offer enterprise grade support as part of <a external-link="true" href="https://www.tiny.cloud/pricing">TinyMCE Enterprise</a>.</li>
  </ul>

  <h2>A simple table to play with</h2>

  <table style="text-align: center;">
    <thead>
      <tr>
        <th>Product</th>
        <th>Cost</th>
        <th>Really?</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>TinyMCE</td>
        <td>Free</td>
        <td>YES!</td>
      </tr>
      <tr>
        <td>Plupload</td>
        <td>Free</td>
        <td>YES!</td>
      </tr>
    </tbody>
  </table>

  <h2>Found a bug?</h2>

  <p>
    If you think you have found a bug please create an issue on the <a external-link="true" href="https://github.com/tinymce/tinymce/issues">GitHub repo</a> to report it to the developers.
  </p>

  <h2>Finally ...</h2>

  <p>
    Don't forget to check out our other product <a external-link="true" href="http://www.plupload.com" target="_blank">Plupload</a>, your ultimate upload solution featuring HTML5 upload support.
  </p>
  <p>
    Thanks for supporting TinyMCE! We hope it helps you and your users create great content.<br>All the best from the TinyMCE team.
  </p>
</textarea>



tinymce.init({
  selector: 'textarea#context-menu',
  height: 500,
  plugins: [
    'link image imagetools table spellchecker'
  ],
  contextmenu: "link image imagetools table spellchecker",
  content_css: [
    '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
    '//www.tiny.cloud/css/codepen.min.css'
  ]
});

(function() { var isIE = !!window.MSInputMethodContext && !!document.documentMode; if (isIE && document.getElementsByClassName(“ie11_optional”)[0] !== undefined) { document.getElementsByClassName(“ie11_optional”)[0].style.display = ‘none’; } })(); (function() { tinymce.init({ selector: ‘textarea#context-menu’, height: 500, plugins: [ ‘link image imagetools table spellchecker’ ], contextmenu: “link image imagetools table spellchecker”, content_css: [ ‘//fonts.googleapis.com/css?family=Lato:300,300i,400,400i’, ‘//www.tiny.cloud/css/codepen.min.css’ ] }); })(); (function() { /* TODO: more js, less jekyll */ var id = “context-menu”; var html = decodeURIComponent(“%0A%3Ctextarea%20id=%22context-menu%22%3E%0A%20%20%3Cp%20style=%22text-align:%20center;%22%3E%0A%20%20%20%20%3Cimg%20title=%22TinyMCE%20Logo%22%20src=%22//www.tiny.cloud/images/glyph-tinymce@2x.png%22%20alt=%22TinyMCE%20Logo%22%20width=%22110%22%20height=%2297%22%20/%3E%0A%20%20%3C/p%3E%0A%0A%20%20%3Ch2%20style=%22text-align:%20center;%22%3EWelcome%20to%20the%20TinyMCE%20editor%20demo!%3C/h2%3E%0A%0A%20%20%3Cp%3E%0A%20%20%20%20Please%20try%20out%20the%20features%20provided%20in%20this%20basic%20example.%3Cbr%3E%0A%20%20%20%20Note%20that%20any%20%3Cstrong%3EMoxieManager%3C/strong%3E%20file%20and%20image%20management%20functionality%20in%20this%20example%20is%20part%20of%20our%20commercial%20offering%20%E2%80%93%20the%20demo%20is%20to%20show%20the%20integration.%0A%20%20%3C/p%3E%0A%0A%20%20%3Ch2%3EGot%20questions%20or%20need%20help?%3C/h2%3E%0A%0A%20%20%3Cul%3E%0A%20%20%20%20%3Cli%3EOur%20%3Ca%20href=%22https://www.tiny.cloud/docs/%22%3Edocumentation%3C/a%3E%20is%20a%20great%20resource%20for%20learning%20how%20to%20configure%20TinyMCE.%3C/li%3E%0A%20%20%20%20%3Cli%3EHave%20a%20specific%20question?%20Visit%20the%20%3Ca%20href=%22https://community.tinymce.com/forum/%22%20target=%22_blank%22%3ECommunity%20Forum%3C/a%3E.%3C/li%3E%0A%20%20%20%20%3Cli%3EWe%20also%20offer%20enterprise%20grade%20support%20as%20part%20of%20%3Ca%20href=%22https://www.tiny.cloud/pricing%22%3ETinyMCE%20Enterprise%3C/a%3E.%3C/li%3E%0A%20%20%3C/ul%3E%0A%0A%20%20%3Ch2%3EA%20simple%20table%20to%20play%20with%3C/h2%3E%0A%0A%20%20%3Ctable%20style=%22text-align:%20center;%22%3E%0A%20%20%20%20%3Cthead%3E%0A%20%20%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Cth%3EProduct%3C/th%3E%0A%20%20%20%20%20%20%20%20%3Cth%3ECost%3C/th%3E%0A%20%20%20%20%20%20%20%20%3Cth%3EReally?%3C/th%3E%0A%20%20%20%20%20%20%3C/tr%3E%0A%20%20%20%20%3C/thead%3E%0A%20%20%20%20%3Ctbody%3E%0A%20%20%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Ctd%3ETinyMCE%3C/td%3E%0A%20%20%20%20%20%20%20%20%3Ctd%3EFree%3C/td%3E%0A%20%20%20%20%20%20%20%20%3Ctd%3EYES!%3C/td%3E%0A%20%20%20%20%20%20%3C/tr%3E%0A%20%20%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Ctd%3EPlupload%3C/td%3E%0A%20%20%20%20%20%20%20%20%3Ctd%3EFree%3C/td%3E%0A%20%20%20%20%20%20%20%20%3Ctd%3EYES!%3C/td%3E%0A%20%20%20%20%20%20%3C/tr%3E%0A%20%20%20%20%3C/tbody%3E%0A%20%20%3C/table%3E%0A%0A%20%20%3Ch2%3EFound%20a%20bug?%3C/h2%3E%0A%0A%20%20%3Cp%3E%0A%20%20%20%20If%20you%20think%20you%20have%20found%20a%20bug%20please%20create%20an%20issue%20on%20the%20%3Ca%20href=%22https://github.com/tinymce/tinymce/issues%22%3EGitHub%20repo%3C/a%3E%20to%20report%20it%20to%20the%20developers.%0A%20%20%3C/p%3E%0A%0A%20%20%3Ch2%3EFinally%20…%3C/h2%3E%0A%0A%20%20%3Cp%3E%0A%20%20%20%20Don’t%20forget%20to%20check%20out%20our%20other%20product%20%3Ca%20href=%22http://www.plupload.com%22%20target=%22_blank%22%3EPlupload%3C/a%3E,%20your%20ultimate%20upload%20solution%20featuring%20HTML5%20upload%20support.%0A%20%20%3C/p%3E%0A%20%20%3Cp%3E%0A%20%20%20%20Thanks%20for%20supporting%20TinyMCE!%20We%20hope%20it%20helps%20you%20and%20your%20users%20create%20great%20content.%3Cbr%3EAll%20the%20best%20from%20the%20TinyMCE%20team.%0A%20%20%3C/p%3E%0A%3C/textarea%3E%0A%0A”); var js = decodeURIComponent(“%0Atinymce.init(%7B%0A%20%20selector:%20’textarea#context-menu’,%0A%20%20height:%20500,%0A%20%20plugins:%20[%0A%20%20%20%20’link%20image%20imagetools%20table%20spellchecker’%0A%20%20],%0A%20%20contextmenu:%20%22link%20image%20imagetools%20table%20spellchecker%22,%0A%20%20content_css:%20[%0A%20%20%20%20’//fonts.googleapis.com/css?family=Lato:300,300i,400,400i’,%0A%20%20%20%20’//www.tiny.cloud/css/codepen.min.css’%0A%20%20]%0A%7D);%0A”); var css = “”; var tabNames = [“run”,”html”,”js”]; /* Note: there are some other fields we could populate here to polish this. */ /* See: https://blog.codepen.io/documentation/api/prefill/ */ var data = { title: “TinyMCE Example”, description: ”, html: html, css: css, css_external: ‘https://www.tiny.cloud/css/codepen.min.css’, js: js, js_external: ‘https://cloud.tinymce.com/5/tinymce.min.js?apiKey=qagffr3pkuv17a8on1afax661irst1hbr4e6tbv888sz91jc’ }; document.getElementById(“codepen_data_context-menu”).value = JSON.stringify(data); /* TODO: */ var tabs = tabNames.map(function(t) { return { tab: document.getElementById(“codepen_tab_” + t + “_” + id), pane: document.getElementById(“codepen_pane_” + t + “_” + id) }; }); tabs.forEach(function(t) { t.tab.onclick = function(e) { tabs.forEach(function(tt) { tt.pane.style.display = t === tt ? ‘block’ : ‘none’; tt.tab.className = t === tt ? ‘codepen_tab_selected’ : ‘codepen_tab_deselected’; }); e.preventDefault(); }; }); if (document.getElementById(“codepen_tab_codepen_” + id) !== null) { document.getElementById(“codepen_tab_codepen_” + id).onclick = function() { document.getElementById(“codepen_form_” + id).submit(); }; } })();

Options

contextmenu

The contextmenu option allows you to specify which items appear on the context menu. The format of this option is a space separated list of items in a string.

The context menu option accepts three styles of item:

  • Any registered menu item.
  • A "|" pipe character to indicate a separator should be added to delineate a group of menu items.
  • Context menu sections defined by a plugin (usually equal to the plugin name). Separators are automatically inserted between context menu sections.

If the same name is registered as both a context menu section and a menu item, the section takes preference.

The default configuration includes all plugins that provide a context menu; link, image, imagetools, table, and spellchecker.

Type: String

Default configuration example

tinymce.init({
  selector: "textarea",
  contextmenu: "link image imagetools table spellchecker"
});

Further examples of the contextmenu option are available in the context menu examples.

contextmenu_never_use_native

The contextmenu_never_use_native option allows you to disable the browser’s native context menu from appearing within the editor.

Caution: Using this option may result into unexpected behavior when right-clicking in text areas. We advise you to consider all your options carefully before using this feature.

Type: Boolean

Default Value: false

Example

tinymce.init({
  selector: "textarea",
  contextmenu_never_use_native: true
});

Registering context menu sections

The structure of context menu sections is a simple query system indexed by name. We strongly recommend using the name of the plugin as the context menu name for ease of configuration.

In the menu shown to the user, sections are delineated by separators. Sections can return an empty array of menu items to indicate that section has no applicable items to the current context and should not be shown.

type ContextMenuApi = {
  update: (element: Element) => string | Array<ContextMenuContents>
}

editor.ui.registry.addContextMenu(name: string, feature: ContextMenuApi);

Every time the user opens the context menu, the selected element is passed to the update function which must return either a space separated string or an array of items to display. The types of the items returned are as follows:

type ContextMenuContents = string | ContextMenuItem | SeparatorMenuItemApi | ContextSubMenu

type ContextMenuItem = {
  type?: 'item';
  text: string;
  icon?: string;
  onAction: () => void;
}
type ContextSubMenu = {
  type: 'submenu';
  text: string;
  icon?: string;
  getSubmenuItems: () => string | Array<ContextMenuContents>;
}

type SeparatorMenuItemApi = {
  type: 'separator'
}

The most common type to use is string, which references an existing registered menu item.

ContextMenuItem, ContextSubMenu and SeparatorMenuItemApi types are intended for use by plugins with completely dynamic menu requirements, where registering each menu item is not necessary. For example, the spellchecker shows a list of suggestions specific to the selected word.

When creating a dynamic menu, the structure type properties are used in order to support untyped API usage:

  • type item (default) is a regular menu item, and must have an onAction method.
  • type submenu must have getSubmenuItems, and if it has an onAction property it is ignored.
  • type separator ignores all other properties.

Defining a context menu section

This example shows how the image plugin dynamically adds the standard image menu section to the context menu. The image context menu section is empty unless the selected element is an image.

TinyMCE HTML JS Edit on CodePen


<textarea id="contextmenu-section">
  <p>Right click on the example image below will show the newly configured context menu.</p>

  <p style="text-align: center;">
    <img title="TinyMCE Logo" src="//www.tiny.cloud/images/glyph-tinymce@2x.png" alt="TinyMCE Logo" width="110" height="97" />
  </p>
  <p>Right Clicking on text should not invoke the context menu</p>
</textarea>



tinymce.PluginManager.add('my-example-plugin', function (editor) {
  editor.ui.registry.addMenuItem('image', {
    icon: 'image',
    text: 'Image',
    onAction: function () {
      console.log('context menu clicked');
      alert('context menu clicked');
    }
  });

  editor.ui.registry.addContextMenu('image', {
    update: function (element) {
      return !element.src ? '' : 'image';
    }
  });
});

tinymce.init({
  selector: "textarea#contextmenu-section",
  contextmenu: "image",
  plugins: 'my-example-plugin'
});


(function() { var isIE = !!window.MSInputMethodContext && !!document.documentMode; if (isIE && document.getElementsByClassName(“ie11_optional”)[0] !== undefined) { document.getElementsByClassName(“ie11_optional”)[0].style.display = ‘none’; } })(); (function() { tinymce.PluginManager.add(‘my-example-plugin’, function (editor) { editor.ui.registry.addMenuItem(‘image’, { icon: ‘image’, text: ‘Image’, onAction: function () { console.log(‘context menu clicked’); alert(‘context menu clicked’); } }); editor.ui.registry.addContextMenu(‘image’, { update: function (element) { return !element.src ? ” : ‘image’; } }); }); tinymce.init({ selector: “textarea#contextmenu-section”, contextmenu: “image”, plugins: ‘my-example-plugin’ }); })(); (function() { /* TODO: more js, less jekyll */ var id = “contextmenu-section”; var html = decodeURIComponent(“%0A%3Ctextarea%20id=%22contextmenu-section%22%3E%0A%20%20%3Cp%3ERight%20click%20on%20the%20example%20image%20below%20will%20show%20the%20newly%20configured%20context%20menu.%3C/p%3E%0A%0A%20%20%3Cp%20style=%22text-align:%20center;%22%3E%0A%20%20%20%20%3Cimg%20title=%22TinyMCE%20Logo%22%20src=%22//www.tiny.cloud/images/glyph-tinymce@2x.png%22%20alt=%22TinyMCE%20Logo%22%20width=%22110%22%20height=%2297%22%20/%3E%0A%20%20%3C/p%3E%0A%20%20%3Cp%3ERight%20Clicking%20on%20text%20should%20not%20invoke%20the%20context%20menu%3C/p%3E%0A%3C/textarea%3E%0A%0A”); var js = decodeURIComponent(“%0Atinymce.PluginManager.add(‘my-example-plugin’,%20function%20(editor)%20%7B%0A%20%20editor.ui.registry.addMenuItem(‘image’,%20%7B%0A%20%20%20%20icon:%20’image’,%0A%20%20%20%20text:%20’Image’,%0A%20%20%20%20onAction:%20function%20()%20%7B%0A%20%20%20%20%20%20console.log(‘context%20menu%20clicked’);%0A%20%20%20%20%20%20alert(‘context%20menu%20clicked’);%0A%20%20%20%20%7D%0A%20%20%7D);%0A%0A%20%20editor.ui.registry.addContextMenu(‘image’,%20%7B%0A%20%20%20%20update:%20function%20(element)%20%7B%0A%20%20%20%20%20%20return%20!element.src%20?%20”%20:%20’image’;%0A%20%20%20%20%7D%0A%20%20%7D);%0A%7D);%0A%0Atinymce.init(%7B%0A%20%20selector:%20%22textarea#contextmenu-section%22,%0A%20%20contextmenu:%20%22image%22,%0A%20%20plugins:%20’my-example-plugin’%0A%7D);%0A%0A”); var css = “”; var tabNames = [“run”,”html”,”js”]; /* Note: there are some other fields we could populate here to polish this. */ /* See: https://blog.codepen.io/documentation/api/prefill/ */ var data = { title: “TinyMCE Example”, description: ”, html: html, css: css, css_external: ‘https://www.tiny.cloud/css/codepen.min.css’, js: js, js_external: ‘https://cloud.tinymce.com/5/tinymce.min.js?apiKey=qagffr3pkuv17a8on1afax661irst1hbr4e6tbv888sz91jc’ }; document.getElementById(“codepen_data_contextmenu-section”).value = JSON.stringify(data); /* TODO: */ var tabs = tabNames.map(function(t) { return { tab: document.getElementById(“codepen_tab_” + t + “_” + id), pane: document.getElementById(“codepen_pane_” + t + “_” + id) }; }); tabs.forEach(function(t) { t.tab.onclick = function(e) { tabs.forEach(function(tt) { tt.pane.style.display = t === tt ? ‘block’ : ‘none’; tt.tab.className = t === tt ? ‘codepen_tab_selected’ : ‘codepen_tab_deselected’; }); e.preventDefault(); }; }); if (document.getElementById(“codepen_tab_codepen_” + id) !== null) { document.getElementById(“codepen_tab_codepen_” + id).onclick = function() { document.getElementById(“codepen_form_” + id).submit(); }; } })();

您暂时不能评论!

管理员开启了需要登录才能够评论,你可以免费注册一个本站的账号。

还没有评论!

目前还没有任何评论,快来抢沙发吧!