This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Add subscript and superscript feature #74
Closed
Closed
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da45e46
add subscript and superscript feature
de199b5
fix tests
230d97c
update
9b8231f
update tests
b5c65e9
update
da032b1
Merge branch 'master' of https://github.com/idleb/ckeditor5-basic-styles
35eb821
update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module basic-styles/subscript | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
import SubscriptEditing from './subscript/subscriptediting'; | ||
import SubscriptUI from './subscript/subscriptui'; | ||
|
||
/** | ||
* The subscript feature. | ||
* | ||
* It loads the {@link module:basic-styles/subscript/subscriptediting~SubscriptEditing} and | ||
* {@link module:basic-styles/subscript/subscriptui~SubscriptUI} plugins. | ||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class Subscript extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
static get requires() { | ||
return [ SubscriptEditing, SubscriptUI ]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
static get pluginName() { | ||
return 'Sub'; | ||
} | ||
} |
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-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module basic-styles/subscript/subscriptediting | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
import AttributeCommand from '../attributecommand'; | ||
|
||
const SUB = 'sub'; | ||
|
||
/** | ||
* The subscript editing feature. | ||
* | ||
* It registers the `sub` command and introduces the `sub` attribute in the model which renders to the view | ||
* as a `<sub>` element. | ||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class SubscriptEditing extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
init() { | ||
const editor = this.editor; | ||
// Allow sub attribute on text nodes. | ||
editor.model.schema.extend( '$text', { allowAttributes: SUB } ); | ||
|
||
// Build converter from model to view for data and editing pipelines. | ||
|
||
editor.conversion.attributeToElement( { | ||
model: SUB, | ||
view: 'sub', | ||
upcastAlso: [ | ||
{ | ||
styles: { | ||
'vertical-align': 'sub' | ||
} | ||
} | ||
] | ||
} ); | ||
|
||
// Create sub command. | ||
editor.commands.add( SUB, new AttributeCommand( editor, SUB ) ); | ||
} | ||
} |
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-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module basic-styles/subscript/subscriptui | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview'; | ||
|
||
import subscriptIcon from '../../theme/icons/subscript.svg'; | ||
|
||
const SUB = 'sub'; | ||
|
||
/** | ||
* The subscript UI feature. It introduces the Subscript button. | ||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class SubscriptUI extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
init() { | ||
const editor = this.editor; | ||
const t = editor.t; | ||
|
||
// Add subscript button to feature components. | ||
editor.ui.componentFactory.add( SUB, locale => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The UI item should be named |
||
const command = editor.commands.get( SUB ); | ||
const view = new ButtonView( locale ); | ||
|
||
view.set( { | ||
label: t( 'Subscript' ), | ||
icon: subscriptIcon, | ||
tooltip: true | ||
} ); | ||
|
||
view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' ); | ||
|
||
// Execute command. | ||
this.listenTo( view, 'execute', () => editor.execute( SUB ) ); | ||
|
||
return view; | ||
} ); | ||
} | ||
} |
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,36 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module basic-styles/superscript | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
import SuperscriptEditing from './superscript/superscriptediting'; | ||
import SuperscriptUI from './superscript/superscriptui'; | ||
|
||
/** | ||
* The superscript feature. | ||
* | ||
* It loads the {@link module:basic-styles/superscript/superscriptediting~SuperscriptEditing} and | ||
* {@link module:basic-styles/superscript/superscriptui~SuperscriptUI} plugins. | ||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class Superscript extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
static get requires() { | ||
return [ SuperscriptEditing, SuperscriptUI ]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
static get pluginName() { | ||
return 'Super'; | ||
} | ||
} |
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-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module basic-styles/superscript/superscriptediting | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
import AttributeCommand from '../attributecommand'; | ||
|
||
const SUPER = 'super'; | ||
|
||
/** | ||
* The superscript editing feature. | ||
* | ||
* It registers the `super` command and introduces the `super` attribute in the model which renders to the view | ||
* as a `<super>` element. | ||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class SuperscriptEditing extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
init() { | ||
const editor = this.editor; | ||
// Allow super attribute on text nodes. | ||
editor.model.schema.extend( '$text', { allowAttributes: SUPER } ); | ||
|
||
// Build converter from model to view for data and editing pipelines. | ||
|
||
editor.conversion.attributeToElement( { | ||
model: SUPER, | ||
view: 'sup', | ||
upcastAlso: [ | ||
{ | ||
styles: { | ||
'vertical-align': 'super' | ||
} | ||
} | ||
] | ||
} ); | ||
|
||
// Create super command. | ||
editor.commands.add( SUPER, new AttributeCommand( editor, SUPER ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
} | ||
} |
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-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module basic-styles/superscript/superscriptui | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview'; | ||
|
||
import superscriptIcon from '../../theme/icons/superscript.svg'; | ||
|
||
const SUPER = 'super'; | ||
|
||
/** | ||
* The superscript UI feature. It introduces the Superscript button. | ||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class SuperscriptUI extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
init() { | ||
const editor = this.editor; | ||
const t = editor.t; | ||
|
||
// Add superscript button to feature components. | ||
editor.ui.componentFactory.add( SUPER, locale => { | ||
const command = editor.commands.get( SUPER ); | ||
const view = new ButtonView( locale ); | ||
|
||
view.set( { | ||
label: t( 'Superscript' ), | ||
icon: superscriptIcon, | ||
tooltip: true | ||
} ); | ||
|
||
view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' ); | ||
|
||
// Execute command. | ||
this.listenTo( view, 'execute', () => editor.execute( SUPER ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
|
||
return view; | ||
} ); | ||
} | ||
} |
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,18 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
import Subscript from '../src/subscript'; | ||
import SubEditing from '../src/subscript/subscriptediting'; | ||
import SubUI from '../src/subscript/subscriptui'; | ||
|
||
describe( 'Subscript', () => { | ||
it( 'should require SubEditing and SubUI', () => { | ||
expect( Subscript.requires ).to.deep.equal( [ SubEditing, SubUI ] ); | ||
} ); | ||
|
||
it( 'should be named', () => { | ||
expect( Subscript.pluginName ).to.equal( 'Sub' ); | ||
} ); | ||
} ); |
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,87 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
import SubEditing from '../../src/subscript/subscriptediting'; | ||
|
||
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; | ||
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; | ||
import AttributeCommand from '../../src/attributecommand'; | ||
|
||
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; | ||
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view'; | ||
|
||
describe( 'SubEditing', () => { | ||
let editor, model; | ||
|
||
beforeEach( () => { | ||
return VirtualTestEditor | ||
.create( { | ||
plugins: [ Paragraph, SubEditing ] | ||
} ) | ||
.then( newEditor => { | ||
editor = newEditor; | ||
model = editor.model; | ||
} ); | ||
} ); | ||
|
||
afterEach( () => { | ||
return editor.destroy(); | ||
} ); | ||
|
||
it( 'should be loaded', () => { | ||
expect( editor.plugins.get( SubEditing ) ).to.be.instanceOf( SubEditing ); | ||
} ); | ||
|
||
it( 'should set proper schema rules', () => { | ||
expect( model.schema.checkAttribute( [ '$root', '$block', '$text' ], 'sub' ) ).to.be.true; | ||
expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'sub' ) ).to.be.true; | ||
} ); | ||
|
||
describe( 'command', () => { | ||
it( 'should register sub command', () => { | ||
const command = editor.commands.get( 'sub' ); | ||
|
||
expect( command ).to.be.instanceOf( AttributeCommand ); | ||
expect( command ).to.have.property( 'attributeKey', 'sub' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'data pipeline conversions', () => { | ||
it( 'should convert <sub> to sub attribute', () => { | ||
editor.setData( '<p><sub>foo</sub>bar</p>' ); | ||
|
||
expect( getModelData( model, { withoutSelection: true } ) ) | ||
.to.equal( '<paragraph><$text sub="true">foo</$text>bar</paragraph>' ); | ||
|
||
expect( editor.getData() ).to.equal( '<p><sub>foo</sub>bar</p>' ); | ||
} ); | ||
|
||
it( 'should convert vertical-align:sub to sub attribute', () => { | ||
editor.setData( '<p><span style="vertical-align: sub;">foo</span>bar</p>' ); | ||
|
||
expect( getModelData( model, { withoutSelection: true } ) ) | ||
.to.equal( '<paragraph><$text sub="true">foo</$text>bar</paragraph>' ); | ||
|
||
expect( editor.getData() ).to.equal( '<p><sub>foo</sub>bar</p>' ); | ||
} ); | ||
|
||
it( 'should be integrated with autoparagraphing', () => { | ||
editor.setData( '<sub>foo</sub>bar' ); | ||
|
||
expect( getModelData( model, { withoutSelection: true } ) ) | ||
.to.equal( '<paragraph><$text sub="true">foo</$text>bar</paragraph>' ); | ||
|
||
expect( editor.getData() ).to.equal( '<p><sub>foo</sub>bar</p>' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'editing pipeline conversion', () => { | ||
it( 'should convert attribute', () => { | ||
setModelData( model, '<paragraph><$text sub="true">foo</$text>bar</paragraph>' ); | ||
|
||
expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><sub>foo</sub>bar</p>' ); | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The subscript command should be named
subscript
, notsub
.