Skip to content

Commit

Permalink
Fix image srcs duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
shibbirweb committed Nov 2, 2020
1 parent f9eac7a commit e719cc3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ckeditor5-image-remove-event-callback-plugin",
"version": "1.1.1",
"version": "1.1.2",
"description": "CKEditor 5 image remove event handler plugin",
"dependencies": {
"@ckeditor/ckeditor5-core": "*",
Expand Down
37 changes: 11 additions & 26 deletions src/ImageRemoveEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default class ImageRemoveEvent {
const editor = this.editor
let model = editor.model

model.document.on('change:data', () => {
const differ = model.document.differ
model.document.on('change:data', (event) => {
const differ = event.source.differ

// if no difference
if (differ.isEmpty) {
Expand Down Expand Up @@ -49,33 +49,18 @@ export default class ImageRemoveEvent {
return;
}

// get any removed image node
const removedFirstImageNode = changes.find(change => (change.type === 'insert' && change.name === 'image'))
// get removed nodes
const removedNodes = changes.filter(change => (change.type === 'insert' && change.name === 'image'))

// get root of removed image node
const root = removedFirstImageNode.position.root;

// get the child count of root
const childCount = root.childCount

// get all removed child nodes
const childNodes = [];

for (let i = 0; i < childCount; i++){
childNodes.push(root.getChild(i))
}

// get all removed images node
const removedImageNodes = childNodes.filter(node => {
return (node.name === 'image')
})

// let removed images src
// removed images src
const removedImagesSrc = [];
// removed image nodes
const removedImageNodes = []

removedImageNodes.forEach(node => {
const src = node.getAttribute('src')
removedImagesSrc.push(src)
removedNodes.forEach(node => {
const removedNode = node.position.nodeAfter
removedImageNodes.push(removedNode)
removedImagesSrc.push(removedNode.getAttribute('src'))
})

// Call the callback
Expand Down

0 comments on commit e719cc3

Please sign in to comment.