Hi All,
I am new to CK-Editor and need to customize image selection dialog to add caption text field, and generate below given markup on selection of image :
<div class='image'>
<img ...>
<div class='image-caption'>Image Caption Text</div>
</div>
I have successfully added caption text field to image selection dialog, but facing issue's with generating markup.
When I add two images I am getting incorrect markup as given below :
<div class="image">
<img ..../>
<div class"image-caption">Caption 1
<div class="image">
<img.... />
<div class="image-caption">Caption 2</div>
</div>
</div>
</div>
Expected Output :
<div class="image">
<img.... />
<div class="image-caption">Caption 1</div>
</div>
<div class="image">
<img.... />
<div class="image-caption">Caption 2</div>
</div>
onOk Handler :
onOk: function ()
{
var D = this;
if (D.imageEditMode)
{
var C = D.imageEditMode;
if (c == 'image' && C == 'input' && confirm(b.lang.image.button2Img))
{
C = 'img';
D.imageElement = b.document.createElement('img');
D.imageElement.setAttribute('alt', '');
b.insertElement(D.imageElement);
}
else if (c != 'image' && C == 'img' && confirm(b.lang.image.img2Button))
{
C = 'input';
D.imageElement = b.document.createElement('input');
D.imageElement.setAttributes({
type: 'image',
alt: ''
});
b.insertElement(D.imageElement);
}
else
{
D.imageElement = D.cleanImageElement;
delete D.cleanImageElement;
}
}
else
{
if (c == 'image')
{
D.imageElement = b.document.createElement('img');
}
else
{
D.imageElement = b.document.createElement('input');
D.imageElement.setAttribute('type', 'image');
}
D.imageElement.setAttribute('alt', '');
}
if (!D.linkEditMode)
{
D.linkElement = b.document.createElement('a');
}
D.commitContent(d, D.imageElement);
D.commitContent(e, D.linkElement);
//Customization start
var data = {};
D.commitContent('caption-value',null,data);
var mainDivElement = b.document.createElement('div');
mainDivElement.setAttribute('class','image');
var captionDivElement = b.document.createElement('div');
if(data.captionValue != null)
{
captionDivElement.setAttribute('class','image-caption');
captionDivElement.appendText(data.captionValue);
}
//Customization end
if (!D.imageElement.getAttribute('style'))
{
D.imageElement.removeAttribute('style');
}
if (!D.imageEditMode)
{
if (D.addLink)
{
if (!D.linkEditMode)
{
b.insertElement(D.linkElement);
D.linkElement.append(D.imageElement, false);
}
else
{
b.insertElement(D.imageElement);
}
}
else
{
//Customized
mainDivElement.append(D.imageElement);
mainDivElement.append(captionDivElement);
b.insertElement(mainDivElement);
}
}
else if (!D.linkEditMode && D.addLink)
{
b.insertElement(D.linkElement);
D.imageElement.appendTo(D.linkElement);
}
else if (D.linkEditMode && !D.addLink)
{
b.getSelection().selectElement(D.linkElement);
b.insertElement(D.imageElement);
}
},
Caption Element Commit Function :
{
id: 'txtCaption',
type: 'text',
label: 'Caption',
accessKey: 'C',
onChange: function () {
l(this.getDialog());
},
setup: function (C, D) {
this.setValue(D.getAttribute('caption'));
},
commit: function (C,D,data)
{
var E = this;
if(C == "caption-value")
{
data.captionValue = E.getValue();
}
if (C == d)
{
if (E.getValue() || E.isChanged())
{
D.setAttribute('caption', E.getValue());
}
}
else if (C == f)
{
D.setAttribute('caption', E.getValue());
}
else if (C == g)
{
D.removeAttribute('caption');
}
}
}
Please let me know possible solution or suggestion to solve this issue.
Thanks !
Faizaan Shaikh.
I am having a similar problem
I am having a similar problem - I have a template which inserts a div containing an image, a title and a paragraph. If I try to insert a second template immediately it is inserted inside the previous div as in the above case. I think the problem is that the focus of the cursor remains inside the div and any further content is added inside it. The question is - how to move the focus outside the first div before adding more content?
Perhaps not the best explanation but the best I can do.
Hi mikejd,
Hi mikejd,
Yes, you are correct focus remains inside current DIV tag and that's the main issue, I have tried some workarounds using rage to refocus outside main DIV but didn't succedded.
Just to inform you my observation, If you add an image and also add an link to it through image selection dialog, It wraps image tag within an anchor tag as given below :
<a ...>
<img.../>
</a>
Now if you add another image or type in some new text it goes ouside of anchor tag as expected the focus goes to right place , but that is not happening in our case where we are using DIV.
I am not sure if it is bug that exists with CK-Editor, or is the case with our custom code.
Let us know if any one has any other observations or suggestions to solve this issue.
Thanks !
Faizaan Shaikh
Hi Faizaan,
Hi Faizaan,
This does seem to be an issue with the use of DIV in the content. I had hoped there was a simple trick to move the focus. It doesn't appear to affect any other tag so maybe it is a bug in CKEditor. Perhaps a moderator can comment? Might be worth posting a bug report.
Mike
Creating more complex
Creating more complex templates may now be a pain. Editor does not know that it should not break an integrity of such template and it isn't possible to block user from doing this.
Therefore you may be interested in checking the new feature we are working on - widgets. In fact, there's a sample of captioned image using figure and figcaption elements - very similar case to yours.
Piotrek (Reinmar) Koszuliński
CKEditor JavaScript Developer
--
CKSource - http://cksource.com
--
Follow CKEditor on: Twitter | Facebook | Google+
Hi reinmar,
Hi reinmar,
Thanks for reply !
Can you please suggest any pointers/links to sample of captioned image using figure and figcaption elements.
Is there any possible solution/approach that comes to your mind to solve above issue.
Thanks,
Faizaan Shaikh
You need to check out branch
You need to check out branch t/9764 from this repository: https://github.com/cksource/ckeditor-dev
The sample is here: https://github.com/cksource/ckeditor-dev/blob/t/9764/plugins/widgetimage/samples/widgetimage.html
However, not that this is a ticket branch and it is not ready yet. In fact, it is not even usable yet. We're planning to introduce widgets in CKEditor 4.2 which should be released in June. Until then you can try to use templates or your previous approach, although it won't be easy.
BTW. If I correctly understand those issues you described in this thread, they are not caused by editor's issues, but a DOM nature. Selection is inside a div and there will be inserted content. To move it outside you need to have another non-empty block there and use Selection API.
Piotrek (Reinmar) Koszuliński
CKEditor JavaScript Developer
--
CKSource - http://cksource.com
--
Follow CKEditor on: Twitter | Facebook | Google+
Hi reinmar,
Hi reinmar,
Thanks a lot for your help, will have a look at your suggested approach, but seems to be a tough task.
Thanks,
Faizaan Shaikh