mirror of
https://github.com/penpot/penpot.git
synced 2025-01-10 08:50:57 -05:00
🐛 Fix problem with pasting text into text editor
This commit is contained in:
parent
7fe419ecb0
commit
9d545004cb
3 changed files with 63 additions and 6 deletions
|
@ -77,6 +77,7 @@
|
||||||
(let [old-blocks (js->clj (.toJS (.getBlockMap (.getCurrentContent ^js old-state)))
|
(let [old-blocks (js->clj (.toJS (.getBlockMap (.getCurrentContent ^js old-state)))
|
||||||
:keywordize-keys false)
|
:keywordize-keys false)
|
||||||
new-blocks (js->clj (.toJS (.getBlockMap (.getCurrentContent ^js state)))
|
new-blocks (js->clj (.toJS (.getBlockMap (.getCurrentContent ^js state)))
|
||||||
|
|
||||||
:keywordize-keys false)]
|
:keywordize-keys false)]
|
||||||
(->> old-blocks
|
(->> old-blocks
|
||||||
(d/mapm
|
(d/mapm
|
||||||
|
@ -173,8 +174,9 @@
|
||||||
handle-return
|
handle-return
|
||||||
(mf/use-callback
|
(mf/use-callback
|
||||||
(fn [_ state]
|
(fn [_ state]
|
||||||
(let [state (ted/editor-split-block state)
|
(let [style (ted/get-editor-current-inline-styles state)
|
||||||
state (handle-change state)]
|
state (-> (ted/insert-text state "\n" style)
|
||||||
|
(handle-change))]
|
||||||
(st/emit! (dwt/update-editor-state shape state)))
|
(st/emit! (dwt/update-editor-state shape state)))
|
||||||
"handled"))
|
"handled"))
|
||||||
|
|
||||||
|
@ -183,7 +185,16 @@
|
||||||
(fn [event]
|
(fn [event]
|
||||||
(when (dom/class? (dom/get-target event) "DraftEditor-root")
|
(when (dom/class? (dom/get-target event) "DraftEditor-root")
|
||||||
(st/emit! (dwt/cursor-to-end shape)))
|
(st/emit! (dwt/cursor-to-end shape)))
|
||||||
(st/emit! (dwt/focus-editor))))]
|
(st/emit! (dwt/focus-editor))))
|
||||||
|
|
||||||
|
handle-pasted-text
|
||||||
|
(fn [text _ editor]
|
||||||
|
(let [style (ted/get-editor-current-inline-styles state)
|
||||||
|
state (-> (ted/insert-text state text style)
|
||||||
|
(handle-change))]
|
||||||
|
(st/emit! (dwt/update-editor-state shape state)))
|
||||||
|
|
||||||
|
"handled")]
|
||||||
|
|
||||||
(mf/use-layout-effect on-mount)
|
(mf/use-layout-effect on-mount)
|
||||||
|
|
||||||
|
@ -203,6 +214,7 @@
|
||||||
:on-focus on-focus
|
:on-focus on-focus
|
||||||
:handle-return handle-return
|
:handle-return handle-return
|
||||||
:strip-pasted-styles true
|
:strip-pasted-styles true
|
||||||
|
:handle-pasted-text handle-pasted-text
|
||||||
:custom-style-fn styles-fn
|
:custom-style-fn styles-fn
|
||||||
:block-renderer-fn #(render-block % shape)
|
:block-renderer-fn #(render-block % shape)
|
||||||
:ref on-editor
|
:ref on-editor
|
||||||
|
|
|
@ -126,3 +126,7 @@
|
||||||
(as-> state $
|
(as-> state $
|
||||||
(reduce redfn $ blocks)
|
(reduce redfn $ blocks)
|
||||||
(impl/setSelection $ selection)))))
|
(impl/setSelection $ selection)))))
|
||||||
|
|
||||||
|
(defn insert-text [state text attrs]
|
||||||
|
(let [style (txt/attrs-to-styles attrs)]
|
||||||
|
(impl/insertText state text (clj->js attrs) (clj->js style))))
|
||||||
|
|
|
@ -9,14 +9,17 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
BlockMapBuilder,
|
||||||
CharacterMetadata,
|
CharacterMetadata,
|
||||||
EditorState,
|
|
||||||
CompositeDecorator,
|
CompositeDecorator,
|
||||||
|
EditorState,
|
||||||
|
Modifier,
|
||||||
|
RichTextEditorUtil,
|
||||||
SelectionState,
|
SelectionState,
|
||||||
Modifier
|
|
||||||
} from "draft-js";
|
} from "draft-js";
|
||||||
|
|
||||||
import {Map} from "immutable";
|
import DraftPasteProcessor from 'draft-js/lib/DraftPasteProcessor';
|
||||||
|
import {Map, OrderedSet} from "immutable";
|
||||||
|
|
||||||
function isDefined(v) {
|
function isDefined(v) {
|
||||||
return v !== undefined && v !== null;
|
return v !== undefined && v !== null;
|
||||||
|
@ -335,3 +338,41 @@ export function getInlineStyle(state, blockKey, offset) {
|
||||||
const block = content.getBlockForKey(blockKey);
|
const block = content.getBlockForKey(blockKey);
|
||||||
return block.getInlineStyleAt(offset).toJS();
|
return block.getInlineStyleAt(offset).toJS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const NEWLINE_REGEX = /\r\n?|\n/g;
|
||||||
|
|
||||||
|
function splitTextIntoTextBlocks(text) {
|
||||||
|
return text.split(NEWLINE_REGEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function insertText(state, text, attrs, inlineStyles) {
|
||||||
|
const blocks = splitTextIntoTextBlocks(text);
|
||||||
|
|
||||||
|
const character = CharacterMetadata.create({style: OrderedSet(inlineStyles)});
|
||||||
|
|
||||||
|
let blockArray = DraftPasteProcessor.processText(
|
||||||
|
blocks,
|
||||||
|
character,
|
||||||
|
"unstyled",
|
||||||
|
);
|
||||||
|
|
||||||
|
blockArray = blockArray.map((b) => {
|
||||||
|
if (b.getText() === "") {
|
||||||
|
return mergeBlockData(b, attrs)
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
});
|
||||||
|
|
||||||
|
const fragment = BlockMapBuilder.createFromArray(blockArray);
|
||||||
|
const content = state.getCurrentContent();
|
||||||
|
const selection = state.getSelection();
|
||||||
|
|
||||||
|
const newContent = Modifier.replaceWithFragment(
|
||||||
|
content,
|
||||||
|
selection,
|
||||||
|
fragment
|
||||||
|
);
|
||||||
|
|
||||||
|
const resultSelection = SelectionState.createEmpty(selection.getStartKey());
|
||||||
|
return EditorState.push(state, newContent, 'insert-fragment');
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue