-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inline Toolbar moving #258
Changes from 1 commit
926f189
6e0771c
1ee8ac1
31e5535
ff40ccc
21a0e6e
effdf1a
64aa5df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -544,4 +544,4 @@ class Blocks { | |
|
||
return instance.get(index); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,37 +8,52 @@ | |
*/ | ||
declare var Module: any; | ||
declare var $: any; | ||
declare var _: any; | ||
import Selection from '../selection'; | ||
|
||
/** | ||
* DOM Elements | ||
*/ | ||
interface InlineToolbarNodes { | ||
wrapper?: Element; // main wrapper | ||
interface INodes { | ||
wrapper?: HTMLElement; // main wrapper | ||
} | ||
|
||
/** | ||
* CSS | ||
*/ | ||
interface InlineToolbarCSS { | ||
interface ICSS { | ||
inlineToolbar: string; | ||
} | ||
|
||
/** | ||
* Coordinates | ||
*/ | ||
interface ICoords { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
export default class InlineToolbar extends Module { | ||
|
||
/** | ||
* Inline Toolbar elements | ||
*/ | ||
private nodes: InlineToolbarNodes = { | ||
private nodes: INodes = { | ||
wrapper: null, | ||
}; | ||
|
||
/** | ||
* CSS styles | ||
*/ | ||
private CSS: InlineToolbarCSS = { | ||
private CSS: ICSS = { | ||
inlineToolbar: 'ce-inline-toolbar', | ||
}; | ||
|
||
/** | ||
* Margin above/below the Toolbar | ||
*/ | ||
private readonly toolbarVerticalMargin: number = 10; | ||
|
||
/** | ||
* @constructor | ||
*/ | ||
|
@@ -62,7 +77,56 @@ export default class InlineToolbar extends Module { | |
|
||
} | ||
|
||
/** | ||
* Move Toolbar to the selected text | ||
*/ | ||
public move() { | ||
// moving | ||
|
||
if (!this.allowedToShow()) { | ||
// close | ||
return; | ||
} | ||
|
||
const selectionCoords: ICoords = Selection.getCoords; | ||
const wrapperOffset = this.getWrapperOffset(); | ||
const toolbarHeight = this.nodes.wrapper.offsetHeight || 40; | ||
|
||
const newCoords: ICoords = { | ||
x: selectionCoords.x - wrapperOffset.left, | ||
y: selectionCoords.y | ||
+ window.scrollY | ||
- wrapperOffset.top | ||
+ toolbarHeight | ||
+ this.toolbarVerticalMargin, | ||
}; | ||
|
||
this.nodes.wrapper.style.left = Math.floor(newCoords.x) + 'px'; | ||
this.nodes.wrapper.style.top = Math.floor(newCoords.y) + 'px'; | ||
|
||
} | ||
|
||
/** | ||
* Need to show Inline Toolbar or not | ||
*/ | ||
private allowedToShow(): boolean { | ||
|
||
/** | ||
* @todo check for empty selection, tagsConflictsWithSelection, currentBlock 'inlineToolbar' settings | ||
*/ | ||
return true; | ||
} | ||
|
||
/** | ||
* Returns editor wrapper offset | ||
* @return {{bottom: number, top: number, left: number, right: number, height: number: width: number}} | ||
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.
|
||
*/ | ||
private getWrapperOffset() { | ||
const rect = this.Editor.UI.nodes.wrapper.getBoundingClientRect(); | ||
|
||
/** | ||
* @todo | ||
* add cache | ||
*/ | ||
return rect; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/** | ||
* Working with selection | ||
* @typedef {Selection} Selection | ||
*/ | ||
export default class Selection { | ||
/** | ||
|
@@ -50,4 +51,73 @@ export default class Selection { | |
|
||
return selection ? selection.isCollapsed : null; | ||
} | ||
} | ||
|
||
/** | ||
* Calculates position of selected text | ||
* @return {{x: number, y: number}} | ||
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. Было бы неплохо выносить общие интерфейсы куда-нибудь. Чтобы здесь можно было написать просто |
||
*/ | ||
static get getCoords() { | ||
let sel = document.selection, range, rect; | ||
let coords = { | ||
x: 0, | ||
y: 0 | ||
}; | ||
|
||
if (sel && sel.type !== 'Control') { | ||
range = sel.createRange(); | ||
range.collapse(true); | ||
coords.x = range.boundingLeft; | ||
coords.y = range.boundingTop; | ||
|
||
return coords; | ||
} | ||
|
||
if (!window.getSelection) { | ||
_.log('Method window.getSelection is not supported', 'warn'); | ||
return coords; | ||
} | ||
|
||
sel = window.getSelection(); | ||
|
||
if (!sel.rangeCount) { | ||
_.log('Method Selection.rangeCount() is not supported', 'warn'); | ||
return coords; | ||
} | ||
|
||
range = sel.getRangeAt(0).cloneRange(); | ||
|
||
if (range.getClientRects) { | ||
range.collapse(true); | ||
|
||
let rects = range.getClientRects(); | ||
|
||
if (rects.length > 0) { | ||
rect = rects[0]; | ||
coords.x = rect.left; | ||
coords.y = rect.top; | ||
} | ||
} | ||
// Fall back to inserting a temporary element | ||
if (coords.x === 0 && coords.y === 0) { | ||
let span = document.createElement('span'); | ||
|
||
if (span.getClientRects) { | ||
// Ensure span has dimensions and position by | ||
// adding a zero-width space character | ||
span.appendChild( document.createTextNode('\u200b') ); | ||
range.insertNode(span); | ||
rect = span.getClientRects()[0]; | ||
coords.x = rect.left; | ||
coords.y = rect.top; | ||
let spanParent = span.parentNode; | ||
|
||
spanParent.removeChild(span); | ||
|
||
// Glue any broken text nodes back together | ||
spanParent.normalize(); | ||
} | ||
} | ||
|
||
return coords; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,4 +168,4 @@ export default class Util { | |
window.setTimeout(() => method.apply(context, args), timeout); | ||
}; | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
.ce-inline-toolbar { | ||
position: absolute; | ||
z-index: 2; | ||
@apply --overlay-pane; | ||
|
||
width: 100px; | ||
height: 40px; | ||
} | ||
transform: translateX(-50%); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"extends": "tslint:recommended", | ||
"rules": { | ||
"quotemark": [true, "single"] | ||
"quotemark": [true, "single"], | ||
"no-console": false | ||
} | ||
} |
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.
может 40 тоже в константу вынесем?