restricteddocumentcommand.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module core/restricteddocumentcommand
  7. */
  8. import Command from './command';
  9. /**
  10. * @extends module:core/command~Command
  11. */
  12. export default class RestrictedDocumentCommand extends Command {
  13. /**
  14. * @inheritDoc
  15. */
  16. refresh() {
  17. const model = this.editor.model;
  18. const doc = model.document;
  19. this.value = !!doc.selection.getAttribute( 'nonRestricted' );
  20. this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'nonRestricted' );
  21. }
  22. /**
  23. * @inheritDoc
  24. */
  25. execute( options = {} ) {
  26. const model = this.editor.model;
  27. const document = model.document;
  28. const selection = document.selection;
  29. const valueToSet = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
  30. model.change( writer => {
  31. const ranges = model.schema.getValidRanges( selection.getRanges(), 'nonRestricted' );
  32. if ( selection.isCollapsed ) {
  33. if ( valueToSet ) {
  34. writer.setSelectionAttribute( 'nonRestricted', true );
  35. } else {
  36. writer.removeSelectionAttribute( 'nonRestricted' );
  37. }
  38. } else {
  39. for ( const range of ranges ) {
  40. if ( valueToSet ) {
  41. writer.setAttribute( 'nonRestricted', valueToSet, range );
  42. } else {
  43. writer.removeAttribute( 'nonRestricted', range );
  44. }
  45. }
  46. }
  47. } );
  48. }
  49. }