Skip to content

Commit

Permalink
MDL-46387 editor-atto: Toggle sub/sup tags using rangy
Browse files Browse the repository at this point in the history
In order to have a consistant browser behavior and prevent nesting of
subscripts and superscripts, tags are converted to CSS classes and then
the CSS classes in the selection are toggeled using the rangy crossbrowser
selection library already included in Atto without using an execCommand.
  • Loading branch information
dthies committed Aug 13, 2015
1 parent dda862a commit dc9ca4a
Show file tree
Hide file tree
Showing 18 changed files with 813 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,55 @@ YUI.add('moodle-atto_subscript-button', function (Y, NAME) {
*/

Y.namespace('M.atto_subscript').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
/**
* A rangy object to alter CSS classes.
*
* @property _subscriptApplier
* @type Object
* @private
*/
_subscriptApplier: null,

/**
* A rangy object to alter CSS classes.
*
* @property _superscriptApplier
* @type Object
* @private
*/
_superscriptApplier: null,

initializer: function() {
this.addBasicButton({
exec: 'subscript',
this.addButton({
buttonName: 'subscript',
callback: this.toggleSubscript,
icon: 'e/subscript',
inlineFormat: true,

// Watch the following tags and add/remove highlighting as appropriate:
tags: 'sub'
});
this._subscriptApplier = window.rangy.createCssClassApplier("editor-subscript");
this._superscriptApplier = window.rangy.createCssClassApplier("editor-superscript");
},

/**
* Toggle subscripts in selection
*
* @method toggleSubscript
*/
toggleSubscript: function() {
// Replace all the sub and sup tags.
this.get('host').changeToCSS('sup', 'editor-superscript');
this.get('host').changeToCSS('sub', 'editor-subscript');

// Remove all superscripts inselection and toggle subscript.
this._superscriptApplier.undoToSelection();
this._subscriptApplier.toggleSelection();

// Replace CSS classes with tags.
this.get('host').changeToTags('editor-subscript', 'sub');
this.get('host').changeToTags('editor-superscript', 'sup');
}
});

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,55 @@ YUI.add('moodle-atto_subscript-button', function (Y, NAME) {
*/

Y.namespace('M.atto_subscript').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
/**
* A rangy object to alter CSS classes.
*
* @property _subscriptApplier
* @type Object
* @private
*/
_subscriptApplier: null,

/**
* A rangy object to alter CSS classes.
*
* @property _superscriptApplier
* @type Object
* @private
*/
_superscriptApplier: null,

initializer: function() {
this.addBasicButton({
exec: 'subscript',
this.addButton({
buttonName: 'subscript',
callback: this.toggleSubscript,
icon: 'e/subscript',
inlineFormat: true,

// Watch the following tags and add/remove highlighting as appropriate:
tags: 'sub'
});
this._subscriptApplier = window.rangy.createCssClassApplier("editor-subscript");
this._superscriptApplier = window.rangy.createCssClassApplier("editor-superscript");
},

/**
* Toggle subscripts in selection
*
* @method toggleSubscript
*/
toggleSubscript: function() {
// Replace all the sub and sup tags.
this.get('host').changeToCSS('sup', 'editor-superscript');
this.get('host').changeToCSS('sub', 'editor-subscript');

// Remove all superscripts inselection and toggle subscript.
this._superscriptApplier.undoToSelection();
this._subscriptApplier.toggleSelection();

// Replace CSS classes with tags.
this.get('host').changeToTags('editor-subscript', 'sub');
this.get('host').changeToTags('editor-superscript', 'sup');
}
});

Expand Down
46 changes: 44 additions & 2 deletions lib/editor/atto/plugins/subscript/yui/src/button/js/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,54 @@
*/

Y.namespace('M.atto_subscript').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
/**
* A rangy object to alter CSS classes.
*
* @property _subscriptApplier
* @type Object
* @private
*/
_subscriptApplier: null,

/**
* A rangy object to alter CSS classes.
*
* @property _superscriptApplier
* @type Object
* @private
*/
_superscriptApplier: null,

initializer: function() {
this.addBasicButton({
exec: 'subscript',
this.addButton({
buttonName: 'subscript',
callback: this.toggleSubscript,
icon: 'e/subscript',
inlineFormat: true,

// Watch the following tags and add/remove highlighting as appropriate:
tags: 'sub'
});
this._subscriptApplier = window.rangy.createCssClassApplier("editor-subscript");
this._superscriptApplier = window.rangy.createCssClassApplier("editor-superscript");
},

/**
* Toggle subscripts in selection
*
* @method toggleSubscript
*/
toggleSubscript: function() {
// Replace all the sub and sup tags.
this.get('host').changeToCSS('sup', 'editor-superscript');
this.get('host').changeToCSS('sub', 'editor-subscript');

// Remove all superscripts inselection and toggle subscript.
this._superscriptApplier.undoToSelection();
this._subscriptApplier.toggleSelection();

// Replace CSS classes with tags.
this.get('host').changeToTags('editor-subscript', 'sub');
this.get('host').changeToTags('editor-superscript', 'sup');
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,55 @@ YUI.add('moodle-atto_superscript-button', function (Y, NAME) {
*/

Y.namespace('M.atto_superscript').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
/**
* A rangy object to alter CSS classes.
*
* @property _subscriptApplier
* @type Object
* @private
*/
_subscriptApplier: null,

/**
* A rangy object to alter CSS classes.
*
* @property _superscriptApplier
* @type Object
* @private
*/
_superscriptApplier: null,

initializer: function() {
this.addBasicButton({
exec: 'superscript',
this.addButton({
buttonName: 'superscript',
inlineFormat: true,
callback: this.toggleSuperscript,
icon: 'e/superscript',

// Watch the following tags and add/remove highlighting as appropriate:
tags: 'sup'
});
this._subscriptApplier = window.rangy.createCssClassApplier("editor-subscript");
this._superscriptApplier = window.rangy.createCssClassApplier("editor-superscript");
},

/**
* Toggle superscripts in selection
*
* @method toggleSuperscript
*/
toggleSuperscript: function() {
// Replace all the sub and sup tags.
this.get('host').changeToCSS('sub', 'editor-subscript');
this.get('host').changeToCSS('sup', 'editor-superscript');

// Remove all subscripts inselection and toggle superscript.
this._superscriptApplier.toggleSelection();
this._subscriptApplier.undoToSelection();

// Replace CSS classes with tags.
this.get('host').changeToTags('editor-superscript', 'sup');
this.get('host').changeToTags('editor-subscript', 'sub');
}
});

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,55 @@ YUI.add('moodle-atto_superscript-button', function (Y, NAME) {
*/

Y.namespace('M.atto_superscript').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
/**
* A rangy object to alter CSS classes.
*
* @property _subscriptApplier
* @type Object
* @private
*/
_subscriptApplier: null,

/**
* A rangy object to alter CSS classes.
*
* @property _superscriptApplier
* @type Object
* @private
*/
_superscriptApplier: null,

initializer: function() {
this.addBasicButton({
exec: 'superscript',
this.addButton({
buttonName: 'superscript',
inlineFormat: true,
callback: this.toggleSuperscript,
icon: 'e/superscript',

// Watch the following tags and add/remove highlighting as appropriate:
tags: 'sup'
});
this._subscriptApplier = window.rangy.createCssClassApplier("editor-subscript");
this._superscriptApplier = window.rangy.createCssClassApplier("editor-superscript");
},

/**
* Toggle superscripts in selection
*
* @method toggleSuperscript
*/
toggleSuperscript: function() {
// Replace all the sub and sup tags.
this.get('host').changeToCSS('sub', 'editor-subscript');
this.get('host').changeToCSS('sup', 'editor-superscript');

// Remove all subscripts inselection and toggle superscript.
this._superscriptApplier.toggleSelection();
this._subscriptApplier.undoToSelection();

// Replace CSS classes with tags.
this.get('host').changeToTags('editor-superscript', 'sup');
this.get('host').changeToTags('editor-subscript', 'sub');
}
});

Expand Down
46 changes: 44 additions & 2 deletions lib/editor/atto/plugins/superscript/yui/src/button/js/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,54 @@
*/

Y.namespace('M.atto_superscript').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
/**
* A rangy object to alter CSS classes.
*
* @property _subscriptApplier
* @type Object
* @private
*/
_subscriptApplier: null,

/**
* A rangy object to alter CSS classes.
*
* @property _superscriptApplier
* @type Object
* @private
*/
_superscriptApplier: null,

initializer: function() {
this.addBasicButton({
exec: 'superscript',
this.addButton({
buttonName: 'superscript',
callback: this.toggleSuperscript,
icon: 'e/superscript',
inlineFormat: true,

// Watch the following tags and add/remove highlighting as appropriate:
tags: 'sup'
});
this._subscriptApplier = window.rangy.createCssClassApplier("editor-subscript");
this._superscriptApplier = window.rangy.createCssClassApplier("editor-superscript");
},

/**
* Toggle superscripts in selection
*
* @method toggleSuperscript
*/
toggleSuperscript: function() {
// Replace all the sub and sup tags.
this.get('host').changeToCSS('sub', 'editor-subscript');
this.get('host').changeToCSS('sup', 'editor-superscript');

// Remove all subscripts inselection and toggle superscript.
this._superscriptApplier.toggleSelection();
this._subscriptApplier.undoToSelection();

// Replace CSS classes with tags.
this.get('host').changeToTags('editor-superscript', 'sup');
this.get('host').changeToTags('editor-subscript', 'sub');
}
});
Loading

0 comments on commit dc9ca4a

Please sign in to comment.