Skip to content

Commit

Permalink
Mouse selection (codex-team#574)
Browse files Browse the repository at this point in the history
* Mouse selection
  • Loading branch information
horoyami authored Feb 19, 2019
1 parent e4b0ca9 commit beeeef0
Show file tree
Hide file tree
Showing 11 changed files with 627 additions and 116 deletions.
1 change: 1 addition & 0 deletions build/codex-editor.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/codex-editor.js

Large diffs are not rendered by default.

70 changes: 35 additions & 35 deletions dist/codex-editor.licenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
regenerator-runtime
MIT

@babel/register
MIT
MIT License

Copyright (c) 2014-2018 Sebastian McKenzie <sebmck@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


codex-notifier
MIT
MIT License
Expand All @@ -127,6 +101,30 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


css-loader
MIT
Copyright JS Foundation and other contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


html-janitor
Apache License
Version 2.0, January 2004
Expand Down Expand Up @@ -380,13 +378,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


css-loader
@babel/register
MIT
Copyright JS Foundation and other contributors
MIT License

Copyright (c) 2014-2018 Sebastian McKenzie <sebmck@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
Expand All @@ -395,10 +395,10 @@ the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 2.9.0

- `Imporvements` - Selection with the mouse is available

### 2.8.1

- `Fix` *Caret* - Fix "History back" call on backspace in Firefox
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codex.editor",
"version": "2.8.1",
"version": "2.9.0",
"description": "CodeX Editor. Native JS, based on API and Open Source",
"main": "dist/codex-editor.js",
"types": "./types/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/components/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export default class Core {
'DragNDrop',
'ModificationsObserver',
'BlockSelection',
'RectangleSelection',
];

await modulesToPrepare.reduce(
Expand Down
144 changes: 83 additions & 61 deletions src/components/modules/blockSelection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @class BlockSelection
* @classdesc Manages Block selection with shortcut CMD+A and with mouse
* @classdesc Manages Block selection with shortcut CMD+A
*
* @module BlockSelection
* @version 1.0.0
Expand All @@ -10,6 +10,7 @@ import _ from '../utils';
import $ from '../dom';

import SelectionUtils from '../selection';
import Block from '../block';

export default class BlockSelection extends Module {

Expand Down Expand Up @@ -44,63 +45,63 @@ export default class BlockSelection extends Module {
};
}

/**
* Flag used to define block selection
* First CMD+A defines it as true and then second CMD+A selects all Blocks
* @type {boolean}
*/
private needToSelectAll: boolean = false;

/**
* Flag used to define native input selection
* In this case we allow double CMD+A to select Block
* @type {boolean}
*/
private nativeInputSelected: boolean = false;

/**
* SelectionUtils instance
* @type {SelectionUtils}
*/
private selection: SelectionUtils;

/**
* Flag that identifies all Blocks selection
* @return {boolean}
*/
public get allBlocksSelected(): boolean {
const { BlockManager } = this.Editor;
const {BlockManager} = this.Editor;

return BlockManager.blocks.every( (block) => block.selected === true);
return BlockManager.blocks.every((block) => block.selected === true);
}

/**
* Set selected all blocks
* @param {boolean} state
*/
public set allBlocksSelected(state: boolean) {
const { BlockManager } = this.Editor;
const {BlockManager} = this.Editor;

BlockManager.blocks.forEach( (block) => block.selected = state);
BlockManager.blocks.forEach((block) => block.selected = state);
}

/**
* Flag that identifies any Block selection
* @return {boolean}
*/
public get anyBlockSelected(): boolean {
const { BlockManager } = this.Editor;
const {BlockManager} = this.Editor;

return BlockManager.blocks.some( (block) => block.selected === true);
return BlockManager.blocks.some((block) => block.selected === true);
}

/**
* Flag used to define block selection
* First CMD+A defines it as true and then second CMD+A selects all Blocks
* @type {boolean}
*/
private needToSelectAll: boolean = false;

/**
* Flag used to define native input selection
* In this case we allow double CMD+A to select Block
* @type {boolean}
*/
private nativeInputSelected: boolean = false;

/**
* SelectionUtils instance
* @type {SelectionUtils}
*/
private selection: SelectionUtils;

/**
* Module Preparation
* Registers Shortcuts CMD+A and CMD+C
* to select all and copy them
*/
public prepare(): void {
const { Shortcuts } = this.Editor;
const {Shortcuts} = this.Editor;

/** Selection shortcut */
Shortcuts.add({
Expand All @@ -113,19 +114,38 @@ export default class BlockSelection extends Module {
this.selection = new SelectionUtils();
}

/**
* Remove selection of Block
* @param {number?} index - Block index according to the BlockManager's indexes
*/
public unSelectBlockByIndex(index?) {
const {BlockManager} = this.Editor;

let block;

if (isNaN(index)) {
block = BlockManager.currentBlock;
} else {
block = BlockManager.getBlockByIndex(index);
}

block.selected = false;
}

/**
* Clear selection from Blocks
*/
public clearSelection(restoreSelection = false) {
this.needToSelectAll = false;
this.nativeInputSelected = false;

if (!this.anyBlockSelected) {
if (!this.anyBlockSelected || this.Editor.RectangleSelection.isRectActivated()) {
this.Editor.RectangleSelection.clearSelection();
return;
}

/**
* restore selection when Block is already selected
* Restore selection when Block is already selected
* but someone tries to write something.
*/
if (restoreSelection) {
Expand All @@ -140,11 +160,11 @@ export default class BlockSelection extends Module {
* Reduce each Block and copy its content
*/
public copySelectedBlocks(): void {
const { BlockManager, Sanitizer } = this.Editor;
const {BlockManager, Sanitizer} = this.Editor;
const fakeClipboard = $.make('div');

BlockManager.blocks.filter( (block) => block.selected )
.forEach( (block) => {
BlockManager.blocks.filter((block) => block.selected)
.forEach((block) => {
/**
* Make <p> tag that holds clean HTML
*/
Expand All @@ -158,13 +178,43 @@ export default class BlockSelection extends Module {
_.copyTextToClipboard(fakeClipboard.innerHTML);
}

/**
* select Block
* @param {number?} index - Block index according to the BlockManager's indexes
*/
public selectBlockByIndex(index?) {
const {BlockManager} = this.Editor;

/**
* Remove previous focused Block's state
*/
BlockManager.clearFocused();

let block;

if (isNaN(index)) {
block = BlockManager.currentBlock;
} else {
block = BlockManager.getBlockByIndex(index);
}

/** Save selection */
this.selection.save();
SelectionUtils.get()
.removeAllRanges();

block.selected = true;
}

/**
* First CMD+A Selects current focused blocks,
* and consequent second CMD+A keypress selects all blocks
*
* @param {keydown} event
*/
private handleCommandA(event): void {
this.Editor.RectangleSelection.clearSelection();

/** allow default selection on native inputs */
if ($.isNativeInput(event.target) && !this.nativeInputSelected) {
this.nativeInputSelected = true;
Expand All @@ -190,32 +240,4 @@ export default class BlockSelection extends Module {
private selectAllBlocks() {
this.allBlocksSelected = true;
}

/**
* select Block
* @param {number?} index - Block index according to the BlockManager's indexes
*/
private selectBlockByIndex(index?) {
const { BlockManager } = this.Editor;

/**
* Remove previous focused Block's state
*/
BlockManager.clearFocused();

let block;

if (isNaN(index)) {
block = BlockManager.currentBlock;
} else {
block = BlockManager.getBlockByIndex(index);
}

/** Save selection */
this.selection.save();
SelectionUtils.get()
.removeAllRanges();

block.selected = true;
}
}
Loading

0 comments on commit beeeef0

Please sign in to comment.