-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Other (image): Encapsulate image replacement into command. Closes #13217
. Other (image): Added `ReplaceImageSourceCommand` which encapsulates current image URL replacement logic. Closes #13217.
- Loading branch information
Showing
4 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/ckeditor5-image/src/image/replaceimagesourcecommand.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
import { Command } from 'ckeditor5/src/core'; | ||
|
||
/** | ||
* @module image/image/replaceimagesourcecommand | ||
*/ | ||
|
||
/** | ||
* Replace image source command. | ||
* | ||
* Changes image source to the one provided. Can be executed as follows: | ||
* | ||
* editor.execute( 'replaceImageSource', { source: 'http://url.to.the/image' } ); | ||
* | ||
* @extends module:core/command~Command | ||
*/ | ||
export default class ReplaceImageSourceCommand extends Command { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
refresh() { | ||
const editor = this.editor; | ||
const imageUtils = editor.plugins.get( 'ImageUtils' ); | ||
const element = this.editor.model.document.selection.getSelectedElement(); | ||
|
||
this.isEnabled = imageUtils.isImage( element ); | ||
this.value = this.isEnabled ? element.getAttribute( 'src' ) : null; | ||
} | ||
|
||
/** | ||
* Executes the command. | ||
* | ||
* @fires execute | ||
* @param {Object} options Options for the executed command. | ||
* @param {String} [options.source] The image source to replace. | ||
*/ | ||
execute( options ) { | ||
const image = this.editor.model.document.selection.getSelectedElement(); | ||
this.editor.model.change( writer => { | ||
writer.setAttribute( 'src', options.source, image ); | ||
writer.removeAttribute( 'srcset', image ); | ||
writer.removeAttribute( 'sizes', image ); | ||
} ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/ckeditor5-image/tests/image/replaceimagesourcecommand.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; | ||
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; | ||
import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; | ||
|
||
import ReplaceImageSourceCommand from '../../src/image/replaceimagesourcecommand'; | ||
import ImageBlockEditing from '../../src/image/imageblockediting'; | ||
import ImageInlineEditing from '../../src/image/imageinlineediting'; | ||
|
||
describe( 'ReplaceImageSourceCommand', () => { | ||
let editor, command, model; | ||
|
||
beforeEach( () => { | ||
return VirtualTestEditor | ||
.create( { | ||
plugins: [ ImageBlockEditing, ImageInlineEditing, Paragraph ] | ||
} ) | ||
.then( newEditor => { | ||
editor = newEditor; | ||
model = editor.model; | ||
|
||
command = new ReplaceImageSourceCommand( editor ); | ||
|
||
const schema = model.schema; | ||
schema.extend( 'imageBlock', { allowAttributes: 'uploadId' } ); | ||
} ); | ||
} ); | ||
|
||
afterEach( () => { | ||
return editor.destroy(); | ||
} ); | ||
|
||
describe( 'execute()', () => { | ||
it( 'should change image source', () => { | ||
setModelData( model, '[<imageBlock src="foo/bar.jpg"></imageBlock>]' ); | ||
|
||
const element = model.document.selection.getSelectedElement(); | ||
|
||
command.execute( { source: 'bar/foo.jpg' } ); | ||
|
||
expect( element.getAttribute( 'src' ) ).to.equal( 'bar/foo.jpg' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'refresh()', () => { | ||
it( 'should be enabled when selected element is an image', () => { | ||
setModelData( model, '[<imageBlock src="foo/bar.jpg"></imageBlock>]' ); | ||
|
||
expect( command.isEnabled ).to.equal( true ); | ||
} ); | ||
|
||
it( 'should not enabled when selected element is not an image', () => { | ||
setModelData( model, '[<paragraph>Foo</paragraph>]' ); | ||
|
||
expect( command.isEnabled ).to.equal( false ); | ||
} ); | ||
|
||
it( 'should store element src value', () => { | ||
setModelData( model, '[<imageBlock src="foo/bar.jpg"></imageBlock>]' ); | ||
|
||
const element = model.document.selection.getSelectedElement(); | ||
|
||
expect( element.getAttribute( 'src' ) ).to.equal( command.value ); | ||
} ); | ||
} ); | ||
} ); |