extending-content-allow-link-target.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals ClassicEditor, console, window, document */
  6. import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config';
  7. function AllowLinkTarget( editor ) {
  8. editor.model.schema.extend( '$text', { allowAttributes: 'linkTarget' } );
  9. editor.conversion.for( 'downcast' ).attributeToElement( {
  10. model: 'linkTarget',
  11. view: ( attributeValue, writer ) => {
  12. const linkElement = writer.createAttributeElement( 'a', { target: attributeValue }, { priority: 5 } );
  13. writer.setCustomProperty( 'link', true, linkElement );
  14. return linkElement;
  15. },
  16. converterPriority: 'low'
  17. } );
  18. editor.conversion.for( 'upcast' ).attributeToAttribute( {
  19. view: {
  20. name: 'a',
  21. key: 'target'
  22. },
  23. model: 'linkTarget',
  24. converterPriority: 'low'
  25. } );
  26. }
  27. ClassicEditor
  28. .create( document.querySelector( '#snippet-link-target' ), {
  29. cloudServices: CS_CONFIG,
  30. extraPlugins: [ AllowLinkTarget ],
  31. toolbar: {
  32. viewportTopOffset: window.getViewportTopOffsetConfig()
  33. }
  34. } )
  35. .then( editor => {
  36. window.editor = editor;
  37. document.querySelector( '#snippet-link-target-content-update' ).addEventListener( 'click', () => {
  38. editor.setData( document.querySelector( '#snippet-link-target-content' ).value );
  39. } );
  40. } )
  41. .catch( err => {
  42. console.error( err.stack );
  43. } );