This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
t/131: Contextual toolbar sample #135
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0c66afe
Contextual toolbar sample.
oleq e04d480
Moved StickyToolbarView manual tests to a dedicated folder.
oleq 97b22c7
Updated sample to the latest API.
oleq fd7f7f1
Simplified contextual toolbar sample.
oleq 090a12f
Hide contextual toolbar when focusTracker says editor is blurred.
oleq dd5ffdd
Multi-line sample.
oleq 0d30445
Added support for multi-line selection.
oleq ef7486c
Some docs and code refactoring.
oleq a5c858e
Clean-up of the sample.
oleq 623451d
Merge branch 'master' into t/131
oleq d50d302
Added static BalloonPanelView's WarrowVerticalOffset and #arrowHorizo…
oleq 5b8e646
Code refactoring in contextual toolbar sample.
oleq 8464f20
Code refactoring in the contextual toolbar sample.
oleq 410d557
Merge branch 'master' into t/131
Reinmar 0af97f0
Fixed: BalloonPanelView.arrow[Horizontal, Vertical]Offset properties …
oleq 33df236
Fixed: Contextual toolbar does not work in Firefox because a mouseup …
oleq df26113
Docs: Clarified docs about balloon positions and their relation with …
oleq 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
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,16 @@ | ||
<head> | ||
<link rel="stylesheet" href="/theme/ckeditor.css"> | ||
</head> | ||
|
||
<div id="editor"> | ||
<p><em>This</em> is a <strong>first line</strong> of text.</p> | ||
<p><em>This</em> is a <strong>second line</strong> of text.</p> | ||
<p><em>This</em> is the <strong>end</strong> of text.</p> | ||
</div> | ||
|
||
<style> | ||
.ck-editor { | ||
margin: 5em auto; | ||
max-width: 70%; | ||
} | ||
</style> |
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,131 @@ | ||
/** | ||
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* globals window, document, console:false */ | ||
|
||
import ClassicEditor from 'ckeditor5/editor-classic/classic.js'; | ||
import DomEventObserver from 'ckeditor5/engine/view/observer/domeventobserver.js'; | ||
import Enter from 'ckeditor5/enter/enter.js'; | ||
import Typing from 'ckeditor5/typing/typing.js'; | ||
import Paragraph from 'ckeditor5/paragraph/paragraph.js'; | ||
import Undo from 'ckeditor5/undo/undo.js'; | ||
import Bold from 'ckeditor5/basic-styles/bold.js'; | ||
import Italic from 'ckeditor5/basic-styles/italic.js'; | ||
|
||
import Template from 'ckeditor5/ui/template.js'; | ||
import ToolbarView from 'ckeditor5/ui/toolbar/toolbarview.js'; | ||
import BalloonPanelView from 'ckeditor5/ui/balloonpanel/balloonpanelview.js'; | ||
|
||
const arrowVOffset = BalloonPanelView.arrowVerticalOffset; | ||
const positions = { | ||
// [text range] | ||
// ^ | ||
// +-----------------+ | ||
// | Balloon | | ||
// +-----------------+ | ||
forwardSelection: ( targetRect, balloonRect ) => ( { | ||
top: targetRect.bottom + arrowVOffset, | ||
left: targetRect.right - balloonRect.width / 2, | ||
name: 's' | ||
} ), | ||
|
||
// +-----------------+ | ||
// | Balloon | | ||
// +-----------------+ | ||
// V | ||
// [text range] | ||
backwardSelection: ( targetRect, balloonRect ) => ( { | ||
top: targetRect.top - balloonRect.height - arrowVOffset, | ||
left: targetRect.left - balloonRect.width / 2, | ||
name: 'n' | ||
} ) | ||
}; | ||
|
||
ClassicEditor.create( document.querySelector( '#editor' ), { | ||
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic ], | ||
toolbar: [ 'bold', 'italic', 'undo', 'redo' ] | ||
} ) | ||
.then( editor => { | ||
createContextualToolbar( editor ); | ||
window.editor = editor; | ||
} ) | ||
.catch( err => { | ||
console.error( err.stack ); | ||
} ); | ||
|
||
function createContextualToolbar( editor ) { | ||
// Create a plain toolbar instance. | ||
const toolbar = new ToolbarView(); | ||
|
||
// Create a BalloonPanelView instance. | ||
const panel = new BalloonPanelView( editor.locale ); | ||
|
||
Template.extend( panel.template, { | ||
attributes: { | ||
class: [ | ||
'ck-toolbar__container', | ||
] | ||
} | ||
} ); | ||
|
||
// Putting the toolbar inside of the balloon panel. | ||
panel.content.add( toolbar ); | ||
|
||
editor.ui.view.body.add( panel ).then( () => { | ||
const editingView = editor.editing.view; | ||
|
||
// Fill the toolbar with some buttons. Simply copy default editor toolbar. | ||
for ( let name of editor.config.get( 'toolbar' ) ) { | ||
toolbar.items.add( editor.ui.componentFactory.create( name ) ); | ||
} | ||
|
||
// Let the focusTracker know about new focusable UI element. | ||
editor.ui.focusTracker.add( panel.element ); | ||
|
||
// Hide the panel when editor loses focus but no the other way around. | ||
panel.listenTo( editor.ui.focusTracker, 'change:isFocused', ( evt, name, is, was ) => { | ||
if ( was && !is ) { | ||
panel.hide(); | ||
} | ||
} ); | ||
|
||
// Add "mouseup" event observer. It's enought to use ClickObserver in Chrome | ||
// but Firefox requires "mouseup" to work properly. | ||
editingView.addObserver( class extends DomEventObserver { | ||
get domEventType() { | ||
return [ 'mouseup' ]; | ||
} | ||
|
||
onDomEvent( domEvent ) { | ||
this.fire( domEvent.type, domEvent ); | ||
} | ||
} ); | ||
|
||
// Position the panel each time the user clicked in editable. | ||
editor.listenTo( editingView, 'mouseup', () => { | ||
// This implementation assumes that only non–collapsed selections gets the contextual toolbar. | ||
if ( !editingView.selection.isCollapsed ) { | ||
const isBackward = editingView.selection.isBackward; | ||
|
||
// getBoundingClientRect() makes no sense when the selection spans across number | ||
// of lines of text. Using getClientRects() allows us to browse micro–ranges | ||
// that would normally make up the bounding client rect. | ||
const rangeRects = editingView.domConverter.viewRangeToDom( editingView.selection.getFirstRange() ).getClientRects(); | ||
|
||
// Select the proper range rect depending on the direction of the selection. | ||
const rangeRect = isBackward ? rangeRects.item( 0 ) : rangeRects.item( rangeRects.length - 1 ); | ||
|
||
panel.attachTo( { | ||
target: rangeRect, | ||
positions: [ | ||
positions[ isBackward ? 'backwardSelection' : 'forwardSelection' ] | ||
] | ||
} ); | ||
} else { | ||
panel.hide(); | ||
} | ||
} ); | ||
} ); | ||
} |
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,5 @@ | ||
## Contextual toolbar demo | ||
|
||
1. Create a non–collapsed selection. | ||
2. Create another non–collapsed selection but in another direction. | ||
3. For each selection, a contextual toolbar should appear and the beginning/end of the selection, duplicating main editor toolbar. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,30 @@ | ||
// Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. | ||
// For licensing, see LICENSE.md or http://ckeditor.com/license | ||
|
||
.ck-toolbar__container { | ||
background: ck-color( 'foreground' ); | ||
|
||
.ck-toolbar { | ||
@include ck-editor-toolbar; | ||
|
||
border: 0; | ||
} | ||
|
||
&.ck-balloon-panel { | ||
&_arrow_s, | ||
&_arrow_se, | ||
&_arrow_sw { | ||
&:after { | ||
border-bottom-color: ck-color( 'foreground' ); | ||
} | ||
} | ||
|
||
&_arrow_n, | ||
&_arrow_ne, | ||
&_arrow_nw { | ||
&:after { | ||
border-top-color: ck-color( 'foreground' ); | ||
} | ||
} | ||
} | ||
} |
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
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.
I'm thinking now... how does this value affect anything? It certainly isn't used anywhere in this code. So perhaps it doesn't need to be defined at all? Am I right that only a feature which wants to understand which position the
getOptimalPosition
function had returned needs to define it?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.
OK, I see that these values are also used in themes. So you should also update
BallonPanelView#position
values. Or even better – extract it to some typedef and document that it's related to the theme's classes.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.
I'm not quite sure what you expect me to change. Positions are documented https://github.com/ckeditor/ckeditor5-utils/blob/master/src/dom/position.js#L263-L272. A position must have a name for any code using the
getOptimalPosition
utility to know, which of the positions have been chosen, not only what coordinates represent it. We've discussed it already.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.
What I'm expecting is that the set of supported names is defined somewhere. Initially I thought that this set is totally open, but it's not – the names must much the positions supported by the theme. So we should indicate (to help the developer understanding what it all is) that this is a set of 6 values and that you can add more if you style them.