imageuploadcommand.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals console */
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import ImageUploadCommand from '../../src/imageupload/imageuploadcommand';
  9. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  10. import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  11. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import Image from '../../src/image/imageediting';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. describe( 'ImageUploadCommand', () => {
  15. let editor, command, model, fileRepository;
  16. class UploadAdapterPluginMock extends Plugin {
  17. init() {
  18. fileRepository = this.editor.plugins.get( FileRepository );
  19. fileRepository.createUploadAdapter = loader => {
  20. return new UploadAdapterMock( loader );
  21. };
  22. }
  23. }
  24. beforeEach( () => {
  25. return VirtualTestEditor
  26. .create( {
  27. plugins: [ FileRepository, Image, Paragraph, UploadAdapterPluginMock ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. model = editor.model;
  32. command = new ImageUploadCommand( editor );
  33. const schema = model.schema;
  34. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  35. } );
  36. } );
  37. afterEach( () => {
  38. sinon.restore();
  39. return editor.destroy();
  40. } );
  41. describe( 'isEnabled', () => {
  42. it( 'should be true when the selection directly in the root', () => {
  43. model.enqueueChange( 'transparent', () => {
  44. setModelData( model, '[]' );
  45. command.refresh();
  46. expect( command.isEnabled ).to.be.true;
  47. } );
  48. } );
  49. it( 'should be true when the selection is in empty block', () => {
  50. setModelData( model, '<paragraph>[]</paragraph>' );
  51. expect( command.isEnabled ).to.be.true;
  52. } );
  53. it( 'should be true when the selection directly in a paragraph', () => {
  54. setModelData( model, '<paragraph>foo[]</paragraph>' );
  55. expect( command.isEnabled ).to.be.true;
  56. } );
  57. it( 'should be true when the selection directly in a block', () => {
  58. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  59. model.schema.extend( '$text', { allowIn: 'block' } );
  60. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  61. setModelData( model, '<block>foo[]</block>' );
  62. expect( command.isEnabled ).to.be.true;
  63. } );
  64. it( 'should be true when the selection is on other image', () => {
  65. setModelData( model, '[<image></image>]' );
  66. expect( command.isEnabled ).to.be.true;
  67. } );
  68. it( 'should be false when the selection is inside other image', () => {
  69. model.schema.register( 'caption', {
  70. allowIn: 'image',
  71. allowContentOf: '$block',
  72. isLimit: true
  73. } );
  74. editor.conversion.for( 'downcast' ).elementToElement( { model: 'caption', view: 'figcaption' } );
  75. setModelData( model, '<image><caption>[]</caption></image>' );
  76. expect( command.isEnabled ).to.be.false;
  77. } );
  78. it( 'should be false when the selection is on other object', () => {
  79. model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
  80. editor.conversion.for( 'downcast' ).elementToElement( { model: 'object', view: 'object' } );
  81. setModelData( model, '[<object></object>]' );
  82. expect( command.isEnabled ).to.be.false;
  83. } );
  84. it( 'should be true when the selection is inside block element inside isLimit element which allows image', () => {
  85. model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
  86. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  87. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
  88. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  89. editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
  90. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tableRow' } );
  91. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'tableCell' } );
  92. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  93. } );
  94. it( 'should be false when schema disallows image', () => {
  95. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  96. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  97. // Block image in block.
  98. model.schema.addChildCheck( ( context, childDefinition ) => {
  99. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  100. return false;
  101. }
  102. } );
  103. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  104. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  105. expect( command.isEnabled ).to.be.false;
  106. } );
  107. } );
  108. describe( 'execute()', () => {
  109. it( 'should insert image at selection position as other widgets', () => {
  110. const file = createNativeFileMock();
  111. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  112. command.execute( { file } );
  113. const id = fileRepository.getLoader( file ).id;
  114. expect( getModelData( model ) )
  115. .to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
  116. } );
  117. it( 'should use parent batch', () => {
  118. const file = createNativeFileMock();
  119. setModelData( model, '<paragraph>[]foo</paragraph>' );
  120. model.change( writer => {
  121. expect( writer.batch.operations ).to.length( 0 );
  122. command.execute( { file } );
  123. expect( writer.batch.operations ).to.length.above( 0 );
  124. } );
  125. } );
  126. it( 'should not insert image nor crash when image could not be inserted', () => {
  127. const file = createNativeFileMock();
  128. model.schema.register( 'other', {
  129. allowIn: '$root',
  130. isLimit: true
  131. } );
  132. model.schema.extend( '$text', { allowIn: 'other' } );
  133. editor.conversion.for( 'downcast' ).elementToElement( { model: 'other', view: 'p' } );
  134. setModelData( model, '<other>[]</other>' );
  135. command.execute( { file } );
  136. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  137. } );
  138. it( 'should not throw when upload adapter is not set (FileRepository will log an warn anyway)', () => {
  139. const file = createNativeFileMock();
  140. fileRepository.createUploadAdapter = undefined;
  141. const consoleWarnStub = sinon.stub( console, 'warn' );
  142. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  143. expect( () => {
  144. command.execute( { file } );
  145. } ).to.not.throw();
  146. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  147. sinon.assert.calledOnce( consoleWarnStub );
  148. } );
  149. } );
  150. } );