forked from Gitlink/forgeplus-react
143 lines
4.0 KiB
JavaScript
143 lines
4.0 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
var _assign = require('object-assign');
|
||
|
|
|
||
|
|
var _extends = _assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
||
|
|
* All rights reserved.
|
||
|
|
*
|
||
|
|
* This source code is licensed under the BSD-style license found in the
|
||
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
|
*
|
||
|
|
* @providesModule DraftTreeAdapter
|
||
|
|
* @format
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* This is unstable and not part of the public API and should not be used by
|
||
|
|
* production systems. This file may be update/removed without notice.
|
||
|
|
*/
|
||
|
|
|
||
|
|
var invariant = require('fbjs/lib/invariant');
|
||
|
|
|
||
|
|
var traverseInDepthOrder = function traverseInDepthOrder(blocks, fn) {
|
||
|
|
var stack = [].concat(blocks).reverse();
|
||
|
|
while (stack.length) {
|
||
|
|
var _block = stack.pop();
|
||
|
|
|
||
|
|
fn(_block);
|
||
|
|
|
||
|
|
var children = _block.children;
|
||
|
|
|
||
|
|
!Array.isArray(children) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Invalid tree raw block') : invariant(false) : void 0;
|
||
|
|
|
||
|
|
stack = stack.concat([].concat(children.reverse()));
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
var isListBlock = function isListBlock(block) {
|
||
|
|
if (!(block && block.type)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
var type = block.type;
|
||
|
|
|
||
|
|
return type === 'unordered-list-item' || type === 'ordered-list-item';
|
||
|
|
};
|
||
|
|
|
||
|
|
var addDepthToChildren = function addDepthToChildren(block) {
|
||
|
|
if (Array.isArray(block.children)) {
|
||
|
|
block.children = block.children.map(function (child) {
|
||
|
|
return child.type === block.type ? _extends({}, child, { depth: (block.depth || 0) + 1 }) : child;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This adapter is intended to be be used as an adapter to draft tree data
|
||
|
|
*
|
||
|
|
* draft state <=====> draft tree state
|
||
|
|
*/
|
||
|
|
var DraftTreeAdapter = {
|
||
|
|
/**
|
||
|
|
* Converts from a tree raw state back to draft raw state
|
||
|
|
*/
|
||
|
|
fromRawTreeStateToRawState: function fromRawTreeStateToRawState(draftTreeState) {
|
||
|
|
var blocks = draftTreeState.blocks;
|
||
|
|
|
||
|
|
var transformedBlocks = [];
|
||
|
|
|
||
|
|
!Array.isArray(blocks) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Invalid raw state') : invariant(false) : void 0;
|
||
|
|
|
||
|
|
if (!Array.isArray(blocks) || !blocks.length) {
|
||
|
|
return draftTreeState;
|
||
|
|
}
|
||
|
|
|
||
|
|
traverseInDepthOrder(blocks, function (block) {
|
||
|
|
var newBlock = _extends({}, block);
|
||
|
|
|
||
|
|
if (isListBlock(block)) {
|
||
|
|
newBlock.depth = newBlock.depth || 0;
|
||
|
|
addDepthToChildren(block);
|
||
|
|
}
|
||
|
|
|
||
|
|
delete newBlock.children;
|
||
|
|
|
||
|
|
transformedBlocks.push(newBlock);
|
||
|
|
});
|
||
|
|
|
||
|
|
draftTreeState.blocks = transformedBlocks;
|
||
|
|
|
||
|
|
return _extends({}, draftTreeState, {
|
||
|
|
blocks: transformedBlocks
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Converts from draft raw state to tree draft state
|
||
|
|
*/
|
||
|
|
fromRawStateToRawTreeState: function fromRawStateToRawTreeState(draftState) {
|
||
|
|
var lastListDepthCacheRef = {};
|
||
|
|
var transformedBlocks = [];
|
||
|
|
|
||
|
|
draftState.blocks.forEach(function (block) {
|
||
|
|
var isList = isListBlock(block);
|
||
|
|
var depth = block.depth || 0;
|
||
|
|
var treeBlock = _extends({}, block, {
|
||
|
|
children: []
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!isList) {
|
||
|
|
// reset the cache path
|
||
|
|
lastListDepthCacheRef = {};
|
||
|
|
transformedBlocks.push(treeBlock);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// update our depth cache reference path
|
||
|
|
lastListDepthCacheRef[depth] = treeBlock;
|
||
|
|
|
||
|
|
// if we are greater than zero we must have seen a parent already
|
||
|
|
if (depth > 0) {
|
||
|
|
var parent = lastListDepthCacheRef[depth - 1];
|
||
|
|
|
||
|
|
!parent ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Invalid depth for RawDraftContentBlock') : invariant(false) : void 0;
|
||
|
|
|
||
|
|
// push nested list blocks
|
||
|
|
parent.children.push(treeBlock);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// push root list blocks
|
||
|
|
transformedBlocks.push(treeBlock);
|
||
|
|
});
|
||
|
|
|
||
|
|
return _extends({}, draftState, {
|
||
|
|
blocks: transformedBlocks
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
module.exports = DraftTreeAdapter;
|