author: Sylvain Viollon
date: Fri Aug 02 13:43:26 2013 +0200
revision: 1102:6952e2316ecd in Products.SilvaExternalSources
branch: 2.4
details: https://hg.infrae.com/Products.SilvaExternalSources?cmd=changeset;node=6952e2316ecd
modified: Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/dialogs/source.js Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/plugin.js
added:
removed:
log: Use new API. Fix errors regarding accessing the current working
source.
diffstat:
Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/dialogs/source.js | 36 +-
Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/plugin.js | 108 +++++----
2 files changed, 78 insertions(+), 66 deletions(-)
diffs (289 lines):
diff -r 1c86691c6b42 -r 6952e2316ecd Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/dialogs/source.js
--- a/Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/dialogs/source.js Thu Aug 01 16:06:21 2013 +0200
+++ b/Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/dialogs/source.js Fri Aug 02 13:43:26 2013 +0200
@@ -310,12 +310,14 @@
container.append(source);
ranges[0].insertNode(container);
- API.loadPreview($(source.$), editor);
+ API.loadPreview(editor, $(source.$));
}
};
});
CKEDITOR.dialog.add('silvaexternalsourceedit', function(editor) {
+ var ALIGNMENT = /^external-source\s+([a-z-]+)\s*$/;
+
return {
title: 'External Source Settings',
minWidth: 600,
@@ -325,30 +327,28 @@
elements: create_parameters_fields('external_source_edit', 0)
}],
onShow: function() {
- var data = {};
- var editor = this.getParentEditor();
- var source = API.getSelectedSource(editor);
- var parse_alignment = /^external-source\s+([a-z-]+)\s*$/;
- var info_alignment = parse_alignment.exec(
- source.getAttribute('class'));
+ var data = {},
+ source = API.getCurrentSource(editor);
- if (info_alignment != null) {
- data.align = info_alignment[1];
- }
- data.name = source.getAttribute('data-silva-name');
- data.instance = source.getAttribute('data-silva-instance');
- if (source.hasAttribute('data-silva-settings')) {
- data.parameters = source.getAttribute('data-silva-settings');
+ if (source !== null) {
+ var alignment = ALIGNMENT.exec(source.getAttribute('class'));
+
+ if (alignment != null) {
+ data.align = alignment[1];
+ }
+ data.name = source.getAttribute('data-silva-name');
+ data.instance = source.getAttribute('data-silva-instance');
+ if (source.hasAttribute('data-silva-settings')) {
+ data.parameters = source.getAttribute('data-silva-settings');
+ };
};
-
this.setupContent(data);
// Reset the title
this.parts.title.setText($.trim(/^[^:]*/.exec(this.parts.title.getText())));
},
onOk: function() {
var data = {},
- editor = this.getParentEditor(),
- source = API.getSelectedSource(editor);
+ source = API.getCurrentSource(editor);
this.commitContent(data);
@@ -356,7 +356,7 @@
source.setAttribute('data-silva-settings', data.parameters);
source.getParent().setAttribute('class', 'inline-container ' + data.align);
- API.loadPreview($(source.$), editor);
+ API.loadPreview(editor, $(source.$));
}
};
});
diff -r 1c86691c6b42 -r 6952e2316ecd Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/plugin.js
--- a/Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/plugin.js Thu Aug 01 16:06:21 2013 +0200
+++ b/Products/SilvaExternalSources/editor/static/plugins/silvaexternalsource/plugin.js Fri Aug 02 13:43:26 2013 +0200
@@ -1,10 +1,10 @@
(function(CKEDITOR, $) {
/**
- * Plugin for External Sources. External Sources inside the editor
- * are represented like this:
+ * CKEditor Plugin for External Sources. External Sources inside
+ * the editor are represented like this:
*
- * <span class="inline-container silva-readonly-element">
+ * <span class="inline-container alignement">
* <div class="alignment external-source">
* <div class="external-source-preview" />
* </div>
@@ -70,18 +70,19 @@
};
return null;
},
- findAndSelectSource: function(editor, node) {
- var source = API.findSource(node),
- wrapper;
- if (source != null) {
- wrapper = source.getParent();
- if (!API.isSourceWrapper(wrapper)) {
- wrapper = source;
- };
- // Select the wrapper if needed.
- CKEDITOR.plugins.silvautils.selectBlock(editor, wrapper);
- return source;
- };
+ setCurrentSource: function(editor, source) {
+ // Save the working source in the editor. This is used to
+ // pass the source to commands.
+ editor._.silvaWorkingSource = source;
+ },
+ getCurrentSource: function(editor) {
+ // Return the source that is currently being
+ // modified. This is used instead of getSelectedSource
+ // because the selection can be changed during the edition
+ // and the reference to the source lost.
+ if (editor._.silvaWorkingSource !== undefined) {
+ return editor._.silvaWorkingSource;
+ }
return null;
},
getSelectedSource: function(editor, no_selection) {
@@ -97,15 +98,15 @@
wrapper = source;
};
// Select the wrapper if needed.
- if (!no_selection && (!selected || wrapper.$ !== selected.$)) {
+ if (!no_selection && wrapper.$ !== selected.$) {
CKEDITOR.plugins.silvautils.selectBlock(editor, wrapper);
};
return source;
};
return null;
},
- loadPreview: function($element, editor) {
- // element is a JQuery element. Editor a CKEditor one.
+ loadPreview: function(editor, $element) {
+ // $element is a JQuery element. Editor a CKEditor one.
var info = $element.attr('data-silva-settings');
var $preview = $element.find('.external-source-preview');
var content_url = $('#content-url').attr('href');
@@ -114,18 +115,18 @@
// We load an existing code source. Add instance and text.
if ($element.attr('data-silva-instance') != undefined) {
extra_info.push({
- 'name': 'source_instance',
- 'value': $element.attr('data-silva-instance')
+ name: 'source_instance',
+ value: $element.attr('data-silva-instance')
});
extra_info.push({
- 'name': 'source_text',
- 'value': editor.name
+ name: 'source_text',
+ value: editor.name
});
} else if ($element.attr('data-silva-name') != undefined) {
// This is a new code source.
extra_info.push({
- 'name': 'source_name',
- 'value': $element.attr('data-silva-name')
+ name: 'source_name',
+ value: $element.attr('data-silva-name')
});
};
// Merge all preview information together.
@@ -133,7 +134,7 @@
info = $.param(extra_info);
} else {
// Those are inline changed options.
- extra_info.push({'name': 'source_inline', 'value': 1});
+ extra_info.push({name: 'source_inline', value: 1});
info += '&' + $.param(extra_info);
};
@@ -157,8 +158,9 @@
$preview.html(html);
// If the document was unmodified, the fact to
// load the preview should not have modified it, reset the flag.
- if (!is_dirty)
+ if (!is_dirty) {
editor.resetDirty();
+ };
}
});
}
@@ -171,6 +173,7 @@
exec: function(editor) {
var source = API.getSelectedSource(editor);
+ API.setCurrentSource(editor, source);
if (source !== null) {
editor.openDialog('silvaexternalsourceedit');
} else {
@@ -202,8 +205,10 @@
};
CKEDITOR.plugins.add('silvaexternalsource', {
- requires: ['dialog'],
+ requires: ['dialog', 'silvautils'],
init: function(editor) {
+ var UTILS = CKEDITOR.plugins.silvautils;
+
editor.addCommand(
'silvaexternalsource',
new CKEDITOR.externalSourceCommand());
@@ -260,11 +265,32 @@
// Events
editor.on('contentDom', function(event) {
// When a document is loaded, we load code sources previews
- var document = $(editor.document.getDocumentElement().$);
+ var $document = $(editor.document.$);
- document.find('.external-source').each(function () {
- API.loadPreview($(this), editor);
+ $document.find('.external-source').each(function () {
+ API.loadPreview(editor, $(this));
});
+ if (!CKEDITOR.env.gecko) {
+ // Help with the selection of the sources.
+ editor.document.on('mousedown', function(event) {
+ var selected,
+ source = API.findSource(event.data.getTarget()),
+ wrapper;
+
+ if (source !== null) {
+ wrapper = source.getParent();
+ if (!API.isSourceWrapper(wrapper)) {
+ wrapper = source;
+ };
+ selected = UTILS.getSelectedElement(editor);
+ if (selected === null || selected.$ !== wrapper.$) {
+ UTILS.selectBlock(editor, wrapper);
+ };
+ // Prevent broken drag'n drop, but not right click.
+ event.data.preventDefault();
+ };
+ });
+ };
});
editor.on('selectionChange', function(event) {
var source = API.getSelectedSource(editor),
@@ -280,8 +306,9 @@
};
});
editor.on('doubleclick', function(event) {
- var source = API.getSelectedSource(editor);
+ var source = API.getSelectedSource(editor, true);
+ API.setCurrentSource(editor, source);
if (source !== null) {
event.data.dialog = 'silvaexternalsourceedit';
};
@@ -292,11 +319,10 @@
var code = event.data.keyCode;
// Improve the navigation before and after the code source with the arrows.
- if (code in {37:1, 38:1, 39:1, 40:1}) {
+ if (code in {9:1, 37:1, 38:1, 39:1, 40:1}) {
setTimeout(function() {
var source = API.getSelectedSource(editor, true),
parent = null,
- target = null,
on_top = code in {37:1, 38:1};
if (source !== null) {
@@ -304,21 +330,7 @@
if (!API.isSourceWrapper(parent)) {
parent = source;
};
-
- if (on_top) {
- target = parent.getPrevious();
- if (target === null) {
- target = editor.document.createElement('p');
- target.insertBefore(parent);
- };
- } else {
- target = parent.getNext();
- if (target === null) {
- target = editor.document.createElement('p');
- target.insertAfter(parent);
- };
- };
- CKEDITOR.plugins.silvautils.selectText(editor, target, on_top);
+ UTILS.selectText(editor, UTILS.getParagraph(editor, parent, on_top), on_top);
};
}, 25);
};
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.