Skip to content

Commit

Permalink
WIKIEDITOR-34: Ability to enable/disable the CodeMirror editor
Browse files Browse the repository at this point in the history
  • Loading branch information
yflory committed Dec 15, 2015
1 parent a4caf91 commit fbd6858
Showing 1 changed file with 30 additions and 4 deletions.
Expand Up @@ -32,7 +32,7 @@
<customClass/>
<contentAuthor>xwiki:XWiki.Admin</contentAuthor>
<creationDate>1450093299000</creationDate>
<date>1450108239000</date>
<date>1450188859000</date>
<contentUpdateDate>1450108212000</contentUpdateDate>
<version>1.1</version>
<title>SyntaxHighlighting</title>
Expand Down Expand Up @@ -119,15 +119,15 @@
<className>XWiki.JavaScriptExtension</className>
<guid>ae324f77-9a39-4a22-b789-fb02e302d54f</guid>
<property>
<cache>long</cache>
<cache>default</cache>
</property>
<property>
<code>#includeMacros('Macros')

## Only do work if the extension is enabled.
#if ($configuration.getValue('enabled') == 1)

define([], function () {
define(['jquery'], function ($) {
var codeMirrors = [];

/* Initialize the CodeMirror library and then start registering the extra modules. */
Expand Down Expand Up @@ -172,12 +172,18 @@ define([], function () {
//
// Attach codeMirrors to all the elements matched with the propertyPattern.
//
var codeMirrorState = window.localStorage['XWiki.extension.SyntaxHighlighting.disable'] || 0;

This comment has been minimized.

Copy link
@Enygma2002

Enygma2002 Dec 15, 2015

What do you want to achieve with this? With the current code, you are setting this preference per user's browser. However, you are enabling/disabling it below per-element, but using the same key, so the effect is that each enable/disable you do does not target the particular element the checkbox is attached to, but the entire page (browser preference actually).

This comment has been minimized.

Copy link
@Enygma2002

Enygma2002 Dec 15, 2015

As I`ve commented on the issue:
"My suggestion is that this should be implemented as a user preference, stored in the user's profile. Makes the most sense."

var textareas = $$(propertyPattern);
for (var i = 0; i&lt;textareas.length; ++i) {
var textarea = textareas[i];
// If the textarea is not visible, like the object editor's textareas are when they are not expanded, delay the initialization
if (textarea.offsetWidth &gt; 0 || textarea.offsetHeight &gt; 0) {
initSingleCodeMirror(textarea, codeMirrorMode);
if(codeMirrorState === '1') {
displayEnableCheckbox(textarea, codeMirrorMode);
}
else {
initSingleCodeMirror(textarea, codeMirrorMode);
}
} else {
var objectDiv = textarea.up('.xobject');
if (!objectDiv) {
Expand All @@ -197,6 +203,16 @@ define([], function () {
}
}

// Add a checkbox to enable CodeMirror for the user
var displayEnableCheckbox = function (textarea, codeMirrorMode) {
var openInstances = $('.enableCodeMirror').length;

This comment has been minimized.

Copy link
@Enygma2002

Enygma2002 Dec 15, 2015

This does not look like a good idea. You will most likely end up with multiple IDs with the same value. What are you using these IDs for anyway?

$('&lt;div class="meta-versionSummary enableCodeMirror"&gt;&lt;input type="checkbox" class="SHcheckbox" id="enableCodeMirror'+openInstances+'"/&gt; &lt;label for="enableCodeMirror'+openInstances+'"&gt;Allow Syntax Highlighting&lt;/label&gt;&lt;/div&gt;').insertAfter(textarea).on('click',function() {

This comment has been minimized.

Copy link
@Enygma2002

Enygma2002 Dec 15, 2015

How does this checkbox look like? Could you provide a screenshot on the issue's page? Adding an extra checkbox next to any textarea might break the display/functionality of certain applications.

Remember that this applies to virtually any textarea that you attach the codemirror instance to... it can be the object editor, wiki editor, wiki macro in inline edit, etc.

initSingleCodeMirror(textarea, codeMirrorMode);
$(this).remove();
window.localStorage['XWiki.extension.SyntaxHighlighting.disable'] = 0;

This comment has been minimized.

Copy link
@Enygma2002

Enygma2002 Dec 15, 2015

Read the note above, what do you want to achieve?

});
}

var initSingleCodeMirror = function (textarea, codeMirrorMode) {
// FIXME: Until a better way comes up, we`ll just reuse the xwiki 2.1 syntax mode for 2.0 as well.
if (codeMirrorMode == 'xwiki/2.0') {
Expand Down Expand Up @@ -328,6 +344,16 @@ define([], function () {

// console.log('CodeMirror instance initialized for mode: ' + codeMirrorMode);

// Add a checkbox to disable CodeMirror for the user
var openInstances = jQuery('.disableCodeMirror').length;
var codeMirrorDiv = jQuery(textarea).siblings('.CodeMirror');
jQuery('&lt;div class="meta-versionSummary disableCodeMirror"&gt;&lt;input type="checkbox" checked="checked" class="SHcheckbox" id="disableCodeMirror'+openInstances+'"/&gt; &lt;label for="disableCodeMirror'+openInstances+'" &gt;Allow Syntax Highlighting&lt;/label&gt;&lt;/div&gt;').insertAfter(codeMirrorDiv).on('click',function() {
codeMirror.toTextArea();
displayEnableCheckbox(textarea, codeMirrorMode);
$(this).remove();
window.localStorage['XWiki.extension.SyntaxHighlighting.disable'] = 1;

This comment has been minimized.

Copy link
@Enygma2002

Enygma2002 Dec 15, 2015

Read the note above, what do you want to achieve?

});

// Track this new instance by adding it to the list of instances.
codeMirrors.push(codeMirror);
}, function (err) {
Expand Down

0 comments on commit fbd6858

Please sign in to comment.