Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

percentage-fontsize plugin implementation #688

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib/SCEditor.js
Expand Up @@ -2250,6 +2250,18 @@ export default function SCEditor(original, userOptions) {
updateActiveButtons();
};

/**
* Returns list of tags from the WYSIWYG editor
*
* @param {string} tagName
* @function
* @name execCommand
* @memberOf SCEditor.prototype
*/
base.wysiwygGetTags = function (tagName) {
return wysiwygDocument.getElementsByTagName(tagName);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns a live ordered collection of Elements by tag name.
Either be explicit about it in its documentation or change the code into something most programmers would expect from it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check other methods, maybe it will be easier to just inject the HTML tags instead of using execCommand

};

/**
* Checks if the current selection has changed and triggers
* the selectionchanged event if it has.
Expand Down
134 changes: 134 additions & 0 deletions src/plugins/percentage-fontsize.js
@@ -0,0 +1,134 @@
/**
* SCEditor Inline-Code Plugin for BBCode format
* http://www.sceditor.com/
*
* Copyright (C) 2011-2013, Sam Clarke (samclarke.com)
*
* SCEditor is licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* @fileoverview SCEditor percentage fontsize plugin for BBCode format
* This plugin allow usage of the percent indication in font size bbcode tag
* in a format used in phpBB system:
* [size=50]text[/font]
* means the text will be shown with font 50% of the current size
* [size=200]text[/font]
* means the text will be shown with font 200% of the current size
* @author Alex Betis
*/

(function (sceditor) {
'use strict';

var dom = sceditor.dom;

sceditor.plugins['percentage-fontsize'] = function () {
var base = this;

/**
* Default sizes
* @type {Object}
* @private
*/
var sizes = [50, 75, 100, 150, 200];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this behave like a constant? If so, turn the variable name into CAPS
Personally, I'd have a default value but allow setting options to change it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. updating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a documentation for plugin configuration fomat?
Couldn't find any documentation for format.js configuration for example...


/**
* Private functions
* @private
*/
var commandHandler;

base.init = function () {
var opts = this.opts;

// Enable for BBCode only
if (opts.format && opts.format !== 'bbcode') {
return;
}

// The plugin will override current fontsize implementation
sceditor.command.set('size', {
exec: commandHandler,
txtExec: commandHandler,
tooltip: 'Font Size'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use _ function to make it translatable or allow it to be translable somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain what do you mean here.
_ for what function?
What does "translatable" means?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a function named _. See: https://www.sceditor.com/api/sceditor/underscore/
"translatable" <=> To be able to have a different text on the node based on the current language

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but I don't understand what function do you mean that have to be with underscore or translatable.
I'm basing my code the same way as is appears in the format plugin that Sam wrote and as defaultCommands.js code looks like...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, OK. The code sends tooltips through the translation function when displaying:
https://github.com/samclarke/SCEditor/blob/master/src/lib/SCEditor.js#L712
I had forgotten about that.

});

sceditor.formats.bbcode.set('size', {
tags: {
font: {
size: null
}
},
styles: {
'font-size': null
},
format: function (element, content) {
var fontSize = dom.attr(element, 'size');
var size;

if (!fontSize) {
fontSize = element.style['font-size'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

element.style['font-size'] -> element.style.fontSize

}

// remove "%" from the option
size = fontSize.replace('%', '');

return '[size=' + size + ']' + content + '[/size]';
},
html: '<font style="font-size:{defaultattr}%">{!0}</font>'
});
};

/**
* Function for the txtExec and exec properties
*
* @param {node} caller
* @private
*/
commandHandler = function (caller) {
var editor = this;
var content = document.createElement('div');

sceditor.utils.each(sizes, function (index, size) {
var link = document.createElement('a');
var font = document.createElement('font');
link.className = 'sceditor-fontsize-option';
link.setAttribute('data-size', size);
font.textContent = size + '%';
font.style.fontSize = size + '%';
link.appendChild(font);
link.addEventListener('click', function (e) {
var size = dom.attr(this, 'data-size');

editor.closeDropDown(true);

if (editor.sourceMode()) {
editor.insert('[size=' + size + ']', '[/size]');
} else {
// execCommand for fontsize accepts only numbers
// from 1 to 7, and it was deprecated!
// We'll set it to 3 and go over all the font tags with
// size 3 and change it to use the style as specified
editor.execCommand('fontsize', 3);

var fontElements = editor.wysiwygGetTags('font');
for (var i = 0,
len = fontElements.length;
i < len; ++i) {
if (fontElements[i].size === '3') {
fontElements[i].removeAttribute('size');
fontElements[i].style.fontSize = size + '%';
}
}
}

e.preventDefault();
});

content.appendChild(link);
});

editor.createDropDown(caller, 'fontsize-picker', content);
};
};
})(sceditor);
1 change: 1 addition & 0 deletions tests/manual/debug/index.html
Expand Up @@ -20,6 +20,7 @@
<script src="../../../src/plugins/undo.js"></script>
<script src="../../../src/plugins/autoyoutube.js"></script>
<script src="../../../src/plugins/format.js"></script>
<script src="../../../src/plugins/percentage-fontsize.js"></script>
<script src="../../../src/plugins/plaintext.js"></script>
<script src="../../../src/plugins/autosave.js"></script>
<script src="../../../src/plugins/dragdrop.js"></script>
Expand Down