@djsp/utils
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,2 +0,3 @@ | ||
import { AtomicBlockUtils, RichUtils, EditorState, Modifier } from 'draft-js'; | ||
import { List } from 'immutable'; | ||
import { AtomicBlockUtils, RichUtils, EditorState, ContentBlock, BlockMapBuilder, genKey, Modifier } from 'draft-js'; | ||
@@ -71,12 +72,73 @@ function replaceWithAtomicBlock(editorState, entityType, data) { | ||
} | ||
function createEntityStrategy(entityType) { | ||
return function entityStrategy(contentBlock, callback, contentState) { | ||
if (!contentState) return; | ||
contentBlock.findEntityRanges(function (character) { | ||
var entityKey = character.getEntity(); | ||
return entityKey !== null && contentState.getEntity(entityKey).getType() === entityType; | ||
}, callback); | ||
}; | ||
} | ||
function getCurrentEntity(editorState) { | ||
var contentState = editorState.getCurrentContent(); | ||
var entityKey = this.getCurrentEntityKey(editorState); | ||
var entityKey = getCurrentEntityKey(editorState); | ||
return entityKey ? contentState.getEntity(entityKey) : null; | ||
} | ||
function getBlockEntityKey(contentState, key) { | ||
var block = contentState.getBlockForKey(key); | ||
var entity = block.getEntityAt(0); | ||
if (entity != null) { | ||
return entity; | ||
} else { | ||
return null; | ||
} | ||
} | ||
function hasEntity(editorState, entityType) { | ||
var entity = this.getCurrentEntity(editorState); | ||
return entity && entity.getType() === entityType; | ||
var entity = getCurrentEntity(editorState); | ||
if (entity) { | ||
return entity.getType() === entityType; | ||
} else { | ||
return false; | ||
} | ||
} | ||
export { replaceWithAtomicBlock, insertEntityBlock, createEntityDecorator, insertTextWithEntity, createLinkAtSelection, removeLinkAtSelection, collapseToEnd, getCurrentEntityKey, getCurrentEntity, hasEntity }; | ||
var insertBlockAfterSelection = function insertBlockAfterSelection(contentState, selectionState, newBlock) { | ||
var targetKey = selectionState.getStartKey(); | ||
var array = []; | ||
contentState.getBlockMap().forEach(function (block, blockKey) { | ||
array.push(block); | ||
if (blockKey !== targetKey) return; | ||
array.push(newBlock); | ||
}); | ||
return contentState.merge({ | ||
blockMap: BlockMapBuilder.createFromArray(array), | ||
selectionBefore: selectionState, | ||
selectionAfter: selectionState.merge({ | ||
anchorKey: newBlock.getKey(), | ||
anchorOffset: newBlock.getLength(), | ||
focusKey: newBlock.getKey(), | ||
focusOffset: newBlock.getLength(), | ||
isBackward: false | ||
}) | ||
}); | ||
}; | ||
function insertNewLine(editorState) { | ||
var contentState = editorState.getCurrentContent(); | ||
var selectionState = editorState.getSelection(); | ||
var newLineBlock = new ContentBlock({ | ||
key: genKey(), | ||
type: 'unstyled', | ||
text: '', | ||
characterList: List() | ||
}); | ||
var withNewLine = insertBlockAfterSelection(contentState, selectionState, newLineBlock); | ||
var newContent = withNewLine.merge({ | ||
selectionAfter: withNewLine.getSelectionAfter().set('hasFocus', true) | ||
}); | ||
return EditorState.push(editorState, newContent, 'insert-fragment'); | ||
} | ||
export { replaceWithAtomicBlock, insertEntityBlock, createEntityDecorator, insertTextWithEntity, createLinkAtSelection, removeLinkAtSelection, collapseToEnd, getCurrentEntityKey, createEntityStrategy, getCurrentEntity, getBlockEntityKey, hasEntity, insertNewLine }; |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var immutable = require('immutable'); | ||
var draftJs = require('draft-js'); | ||
@@ -76,12 +77,73 @@ | ||
} | ||
function createEntityStrategy(entityType) { | ||
return function entityStrategy(contentBlock, callback, contentState) { | ||
if (!contentState) return; | ||
contentBlock.findEntityRanges(function (character) { | ||
var entityKey = character.getEntity(); | ||
return entityKey !== null && contentState.getEntity(entityKey).getType() === entityType; | ||
}, callback); | ||
}; | ||
} | ||
function getCurrentEntity(editorState) { | ||
var contentState = editorState.getCurrentContent(); | ||
var entityKey = this.getCurrentEntityKey(editorState); | ||
var entityKey = getCurrentEntityKey(editorState); | ||
return entityKey ? contentState.getEntity(entityKey) : null; | ||
} | ||
function getBlockEntityKey(contentState, key) { | ||
var block = contentState.getBlockForKey(key); | ||
var entity = block.getEntityAt(0); | ||
if (entity != null) { | ||
return entity; | ||
} else { | ||
return null; | ||
} | ||
} | ||
function hasEntity(editorState, entityType) { | ||
var entity = this.getCurrentEntity(editorState); | ||
return entity && entity.getType() === entityType; | ||
var entity = getCurrentEntity(editorState); | ||
if (entity) { | ||
return entity.getType() === entityType; | ||
} else { | ||
return false; | ||
} | ||
} | ||
var insertBlockAfterSelection = function insertBlockAfterSelection(contentState, selectionState, newBlock) { | ||
var targetKey = selectionState.getStartKey(); | ||
var array = []; | ||
contentState.getBlockMap().forEach(function (block, blockKey) { | ||
array.push(block); | ||
if (blockKey !== targetKey) return; | ||
array.push(newBlock); | ||
}); | ||
return contentState.merge({ | ||
blockMap: draftJs.BlockMapBuilder.createFromArray(array), | ||
selectionBefore: selectionState, | ||
selectionAfter: selectionState.merge({ | ||
anchorKey: newBlock.getKey(), | ||
anchorOffset: newBlock.getLength(), | ||
focusKey: newBlock.getKey(), | ||
focusOffset: newBlock.getLength(), | ||
isBackward: false | ||
}) | ||
}); | ||
}; | ||
function insertNewLine(editorState) { | ||
var contentState = editorState.getCurrentContent(); | ||
var selectionState = editorState.getSelection(); | ||
var newLineBlock = new draftJs.ContentBlock({ | ||
key: draftJs.genKey(), | ||
type: 'unstyled', | ||
text: '', | ||
characterList: immutable.List() | ||
}); | ||
var withNewLine = insertBlockAfterSelection(contentState, selectionState, newLineBlock); | ||
var newContent = withNewLine.merge({ | ||
selectionAfter: withNewLine.getSelectionAfter().set('hasFocus', true) | ||
}); | ||
return draftJs.EditorState.push(editorState, newContent, 'insert-fragment'); | ||
} | ||
exports.replaceWithAtomicBlock = replaceWithAtomicBlock; | ||
@@ -95,3 +157,6 @@ exports.insertEntityBlock = insertEntityBlock; | ||
exports.getCurrentEntityKey = getCurrentEntityKey; | ||
exports.createEntityStrategy = createEntityStrategy; | ||
exports.getCurrentEntity = getCurrentEntity; | ||
exports.getBlockEntityKey = getBlockEntityKey; | ||
exports.hasEntity = hasEntity; | ||
exports.insertNewLine = insertNewLine; |
{ | ||
"name": "@djsp/utils", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Utilities for draft js", | ||
@@ -25,2 +25,3 @@ "author": "juliankrispel", | ||
"devDependencies": { | ||
"@djsp/core": "^0.1.1", | ||
"cross-env": "^5.1.4", | ||
@@ -27,0 +28,0 @@ "flow-bin": "^0.81.0", |
@@ -5,2 +5,3 @@ # @djsp/utils | ||
![file size](http://img.badgesize.io/https://unpkg.com/@djsp/utils/dist/index.js?label=size&style=flat-square) | ||
[![NPM](https://img.shields.io/npm/v/@djsp/utils.svg)](https://www.npmjs.com/package/@djsp/utils) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
@@ -7,0 +8,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13919
288
33
9