Skip to content
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

Replace current block if it is empty #320

Merged
merged 1 commit into from
Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions build/codex-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17324,12 +17324,7 @@ var BlockManager = function (_Module) {

var block = this.composeBlock(toolName, data, settings);

/** If current Block is empty and new Block is not empty, replace current Block with new one */
if (this.currentBlock && this.currentBlock.isEmpty && !block.isEmpty) {
this._blocks.insert(this.currentBlockIndex, block, true);
} else {
this._blocks[++this.currentBlockIndex] = block;
}
this._blocks[++this.currentBlockIndex] = block;
this.Editor.Caret.setToBlock(block);

return block;
Expand Down Expand Up @@ -18974,13 +18969,13 @@ var Paste = function (_Module) {
_this.splitBlock();
_context2.next = 21;
return Promise.all(dataToInsert.map(function () {
var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(data) {
var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(data, i) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return _this.insertBlock(data);
return _this.insertBlock(data, i === 0);

case 2:
return _context.abrupt('return', _context.sent);
Expand All @@ -18993,7 +18988,7 @@ var Paste = function (_Module) {
}, _callee, _this2);
}));

return function (_x2) {
return function (_x2, _x3) {
return _ref7.apply(this, arguments);
};
}()));
Expand Down Expand Up @@ -19121,7 +19116,11 @@ var Paste = function (_Module) {
}

this.splitBlock();
BlockManager.insert(blockData.tool, blockData.data);
if (BlockManager.currentBlock.isEmpty) {
BlockManager.replace(blockData.tool, blockData.data);
} else {
BlockManager.insert(blockData.tool, blockData.data);
}
return _context4.abrupt('return');

case 11:
Expand All @@ -19136,7 +19135,7 @@ var Paste = function (_Module) {
}, _callee4, this);
}));

function processSingleBlock(_x3) {
function processSingleBlock(_x4) {
return _ref9.apply(this, arguments);
}

Expand Down Expand Up @@ -19193,7 +19192,7 @@ var Paste = function (_Module) {
}, _callee5, this);
}));

function processPattern(_x4) {
function processPattern(_x5) {
return _ref10.apply(this, arguments);
}

Expand All @@ -19202,14 +19201,16 @@ var Paste = function (_Module) {
/**
*
* @param {IPasteData} data
* @param {Boolean} canReplaceCurrentBlock - if true and is current Block is empty, will replace current Block
* @returns {Promise<void>}
*/

}, {
key: 'insertBlock',
value: function () {
var _ref11 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(data) {
var blockData, BlockManager;
var canReplaceCurrentBlock = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var blockData, BlockManager, currentBlock;
return regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
Expand All @@ -19220,18 +19221,28 @@ var Paste = function (_Module) {
case 2:
blockData = _context6.sent;
BlockManager = this.Editor.BlockManager;
currentBlock = BlockManager.currentBlock;

if (!(canReplaceCurrentBlock && currentBlock.isEmpty)) {
_context6.next = 8;
break;
}

BlockManager.replace(data.tool, blockData);
return _context6.abrupt('return');

case 8:
BlockManager.insert(data.tool, blockData);

case 5:
case 9:
case 'end':
return _context6.stop();
}
}
}, _callee6, this);
}));

function insertBlock(_x5) {
function insertBlock(_x7) {
return _ref11.apply(this, arguments);
}

Expand Down
2 changes: 1 addition & 1 deletion build/codex-editor.js.map

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions src/components/modules/blockManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ export default class BlockManager extends Module {
let block = this.composeBlock(toolName, data, settings);


/** If current Block is empty and new Block is not empty, replace current Block with new one */
if (this.currentBlock && this.currentBlock.isEmpty && !block.isEmpty) {
this._blocks.insert(this.currentBlockIndex, block, true);
} else {
this._blocks[++this.currentBlockIndex] = block;
}
this._blocks[++this.currentBlockIndex] = block;
this.Editor.Caret.setToBlock(block);

return block;
Expand Down
19 changes: 16 additions & 3 deletions src/components/modules/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ export default class Paste extends Module {

this.splitBlock();

await Promise.all(dataToInsert.map(async (data) => await this.insertBlock(data)));
await Promise.all(dataToInsert.map(
async (data, i) => await this.insertBlock(data, i === 0),
));

Caret.setToBlock(BlockManager.currentBlock, 0, true);
}
Expand All @@ -267,7 +269,11 @@ export default class Paste extends Module {
if (blockData) {
this.splitBlock();

BlockManager.insert(blockData.tool, blockData.data);
if (BlockManager.currentBlock.isEmpty) {
BlockManager.replace(blockData.tool, blockData.data);
} else {
BlockManager.insert(blockData.tool, blockData.data);
}
return;
}
}
Expand Down Expand Up @@ -304,11 +310,18 @@ export default class Paste extends Module {
/**
*
* @param {IPasteData} data
* @param {Boolean} canReplaceCurrentBlock - if true and is current Block is empty, will replace current Block
* @returns {Promise<void>}
*/
private async insertBlock(data: IPasteData): Promise<void> {
private async insertBlock(data: IPasteData, canReplaceCurrentBlock: boolean = false): Promise<void> {
const blockData = await data.handler(data.content);
const {BlockManager} = this.Editor;
const {currentBlock} = BlockManager;

if (canReplaceCurrentBlock && currentBlock.isEmpty) {
BlockManager.replace(data.tool, blockData);
return;
}

BlockManager.insert(data.tool, blockData);
}
Expand Down