Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Unsticky inline styles #83

Merged
merged 3 commits into from
May 24, 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
35 changes: 34 additions & 1 deletion src/__test__/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("draft-js-markdown-plugin", () => {
expect(subject()).toBe("not-handled");
});

it("resets curent inline style", () => {
it("resets current inline style", () => {
currentRawContentState = {
entityMap: {},
blocks: [
Expand Down Expand Up @@ -545,6 +545,39 @@ describe("draft-js-markdown-plugin", () => {
" "
);
});
it("unstickys inline style", () => {
currentRawContentState = {
entityMap: {},
blocks: [
{
key: "item1",
text: "item1",
type: "unstyled",
depth: 0,
inlineStyleRanges: [
{ offset: 0, length: 5, style: "BOLD" },
],
entityRanges: [],
data: {},
},
],
};

currentSelectionState = currentSelectionState.merge({
focusOffset: 5,
anchorOffset: 5,
});

expect(subject()).toBe("handled");
expect(store.setEditorState).toHaveBeenCalled();
newEditorState = store.setEditorState.mock.calls[0][0];
const block = newEditorState.getCurrentContent().getLastBlock();
const length = block.getLength();
expect(block.getInlineStyleAt(length - 1).toJS()).toEqual([]);
expect(block.getInlineStyleAt(length - 2).toJS()).toEqual([
"BOLD",
]);
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also add some unit tests for when stickiness doesn't apply: like when you're in an entity for example 👍 just to make sure it doesn't break in future

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried for like an hour to write another test, but it always has the wrong content state (text: altered!!) and I don't know where it's coming from or how to fix it 😢 Any clues?

});
});
describe("character is not a space", () => {
Expand Down
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@ function checkReturnForState(config, editorState, ev) {
return newEditorState;
}

const unstickyInlineStyles = (character, editorState) => {
const selection = editorState.getSelection();
if (!selection.isCollapsed()) return editorState;

const startOffset = selection.getStartOffset();
const content = editorState.getCurrentContent();
const block = content.getBlockForKey(selection.getStartKey());
const entity = block.getEntityAt(startOffset - 1);
if (entity !== null) return editorState;

// If we're currently in a style, but the next character doesn't have a style (or doesn't exist)
// we insert the characters manually without the inline style to "unsticky" them
const style = block.getInlineStyleAt(startOffset - 1);
if (style.size === 0) return editorState;
const nextStyle = block.getInlineStyleAt(startOffset);
if (nextStyle.size !== 0) return editorState;

const newContent = Modifier.insertText(content, selection, character);
return EditorState.push(editorState, newContent, "insert-characters");
};

const defaultConfig = {
renderLanguageSelect: defaultRenderSelect,
languages: defaultLanguages,
Expand Down Expand Up @@ -347,6 +368,12 @@ const createMarkdownPlugin = (_config = {}) => {
// If we're in a link - don't transform markdown
if (inLink(editorState)) return "not-handled";

const unsticky = unstickyInlineStyles(character, editorState);
if (editorState !== unsticky) {
setEditorState(unsticky);
return "handled";
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to add a bunch of unit tests for this 👍


const newEditorState = checkCharacterForState(
config,
editorState,
Expand Down