mirror of
https://github.com/penpot/penpot.git
synced 2025-05-29 00:26:19 +02:00
🐛 Proper handle visual selection on blured editor.
This commit is contained in:
parent
5519cdfd7c
commit
f0087e11b0
5 changed files with 202 additions and 52 deletions
57
frontend/src/app/util/draft_helpers.js
Normal file
57
frontend/src/app/util/draft_helpers.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
/**
|
||||
* Copyright (c) UXBOX Labs SL
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
import {CharacterMetadata} from "draft-js";
|
||||
import {Map} from "immutable";
|
||||
|
||||
function removeStylePrefix(chmeta, stylePrefix) {
|
||||
var withoutStyle = chmeta.set('style', chmeta.getStyle().filter((s) => !s.startsWith(stylePrefix)))
|
||||
return CharacterMetadata.create(withoutStyle);
|
||||
};
|
||||
|
||||
export function removeInlineStylePrefix(contentState, selectionState, stylePrefix) {
|
||||
var blockMap = contentState.getBlockMap();
|
||||
var startKey = selectionState.getStartKey();
|
||||
var startOffset = selectionState.getStartOffset();
|
||||
var endKey = selectionState.getEndKey();
|
||||
var endOffset = selectionState.getEndOffset();
|
||||
var newBlocks = blockMap.skipUntil(function (_, k) {
|
||||
return k === startKey;
|
||||
}).takeUntil(function (_, k) {
|
||||
return k === endKey;
|
||||
}).concat(Map([[endKey, blockMap.get(endKey)]])).map(function (block, blockKey) {
|
||||
var sliceStart;
|
||||
var sliceEnd;
|
||||
|
||||
if (startKey === endKey) {
|
||||
sliceStart = startOffset;
|
||||
sliceEnd = endOffset;
|
||||
} else {
|
||||
sliceStart = blockKey === startKey ? startOffset : 0;
|
||||
sliceEnd = blockKey === endKey ? endOffset : block.getLength();
|
||||
}
|
||||
|
||||
var chars = block.getCharacterList();
|
||||
var current;
|
||||
|
||||
while (sliceStart < sliceEnd) {
|
||||
current = chars.get(sliceStart);
|
||||
chars = chars.set(sliceStart, removeStylePrefix(current, stylePrefix));
|
||||
sliceStart++;
|
||||
}
|
||||
|
||||
return block.set('characterList', chars);
|
||||
});
|
||||
|
||||
return contentState.merge({
|
||||
blockMap: blockMap.merge(newBlocks),
|
||||
selectionBefore: selectionState,
|
||||
selectionAfter: selectionState
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue