medium-editor icon indicating copy to clipboard operation
medium-editor copied to clipboard

Tab spaces

Open manishmshivanandhan opened this issue 11 years ago • 5 comments

Any directions on how to enable tab spaces in the .editable class ?

manishmshivanandhan avatar Nov 25 '15 12:11 manishmshivanandhan

@manishmshiva Do you mean to add spaces by pressing tab key ? If yes, I do not think there is any option to do that.

rishijain avatar Nov 28 '15 05:11 rishijain

If as @rishijain said, you're wanting to add the Tab character into the content of the editor i.e. /t - some extra functionality will need to be added. The default function of the TAB key when in a textarea is to move the cursor to the next available input.

If you look at the bindTab function here; https://github.com/createdotnet/medium-editor/blob/master/dist/js/medium-editor.js#L464-L487

There's some functionality to allow tabs in <pre> blocks, and to indent lists items.

Essentially if you were to remove the if statement; if (tag === 'pre') { - you should be able to insert tabs.

Dayjo avatar Feb 13 '16 01:02 Dayjo

The code that @Dayjo is talking about is now here in the latest master.

nmielnik avatar Feb 13 '16 18:02 nmielnik

This is an extension that will indent and unindent using the tab key except when inside a pre block.

(function(root, factory) {
  'use strict';
  if (typeof module === 'object') {
    module.exports = factory;
  } else if (typeof define === 'function' && define.amd) {
    define(factory);
  } else {
    root.TabIndent = factory;
  }
}(this, function(MediumEditor) {

  var TabIndent = MediumEditor.Extension.extend({
    name: 'tabIndent',
    init: function() {
      this.subscribe('editableKeydownTab', this.handleTabKeydown.bind(this));
    },
    handleTabKeydown: function(event) {
      var node = MediumEditor.selection.getSelectionStart(this.base.options.ownerDocument),
        tag = node && node.nodeName.toLowerCase();

      // Excluding default Tab functionality
      if (!MediumEditor.util.isListItem(node) && tag !== 'pre') {
        event.preventDefault();

        // If Shift is down, outdent, otherwise indent
        if (event.shiftKey) {
          this.base.options.ownerDocument.execCommand('outdent', false, null);
        } else {
          this.base.options.ownerDocument.execCommand('indent', false, null);
        }
      }
    },
  });

  return TabIndent;

}(typeof require === 'function'
  ? require('medium-editor')
  : MediumEditor)));

Example usage:

var tabIndent = new TabIndent();
var editor = new MediumEditor('.editable', {
    buttonLabels: 'fontawesome',
    extensions: {
      'tabIndent': tabIndent,
    },
  })

aahoo avatar Nov 13 '16 16:11 aahoo

If @aahoo or anyone would like to build this out as an actual extension in a separate repo/npm package, we'd be happy to mention it in our list of known extensions in the documentation and our landing-page.

Otherwise, please use the above code as a reference for how to add this into your project.

Thanks for sharing @aahoo !

nmielnik avatar Feb 08 '17 05:02 nmielnik