Skip to content

Commit

Permalink
feat: added richtext support for pagevariables
Browse files Browse the repository at this point in the history
  • Loading branch information
JorisPannekeet committed Mar 7, 2024
1 parent 2a86a36 commit 81702c7
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions src/components/richTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,32 @@
floatLabel,
} = options;
const isDev = env === 'dev';

const [currentValue, setCurrentValue] = useState(
useText(valueProp, { rawValue: true }),
);
const optionValue = useText(valueProp, { rawValue: true });
const [currentValue, setCurrentValue] = useState(optionValue);
const [valueKey, setValueKey] = useState(0);
const labelText = useText(label);
const [showDropdown, setShowDropdown] = useState(false);
const [multipleStyles, setMultipleStyles] = useState(false);
const [activeStyleName, setActiveStyleName] = useState('Body 1');
const placeholderText = useText(placeholder);
const helperTextResolved = useText(helperText);

useEffect(() => {
setCurrentValue(optionValue);
setValueKey(valueKey + 1);
}, [optionValue]);

const KeyCode = {
Digit1: 49,
Digit2: 50,
Digit3: 51,
Digit4: 52,
Digit5: 53,
Digit6: 54,
Digit7: 55,
Digit8: 56,
};

const isMarkActive = (editor, format) => {
const marks = Editor.marks(editor);
return marks ? marks[format] === true : false;
Expand Down Expand Up @@ -381,7 +397,18 @@
return <span {...attributes}>{children}</span>;
}

const checkMultipleStyles = (value) => {
const diverseStyle = value.filter(({ type }) => type !== value[0].type);
return diverseStyle.length > 0;
};

const onChangeHandler = (value) => {
if (value.length > 1) {
const hasMultipleStyles = checkMultipleStyles(value);
if (hasMultipleStyles) {
setMultipleStyles(true);
}
}
setCurrentValue(value.map((row) => serialize(row)).join(''));
B.triggerEvent('onChange', currentValue);
};
Expand Down Expand Up @@ -527,35 +554,32 @@

if (event.altKey) {
switch (event.keyCode) {
case 49:
case KeyCode.Digit1:
event.preventDefault();
toggleBlock(editor, 'heading-one');
break;
case 50:
case KeyCode.Digit2:
event.preventDefault();
toggleBlock(editor, 'heading-two');
break;
case 51:
case KeyCode.Digit3:
event.preventDefault();
toggleBlock(editor, 'heading-three');
break;
case 52:
case KeyCode.Digit4:
event.preventDefault();
toggleBlock(editor, 'heading-four');
break;
case 53:
case KeyCode.Digit5:
event.preventDefault();
toggleBlock(editor, 'heading-five');
break;
case 54:
case KeyCode.Digit6:
event.preventDefault();
toggleBlock(editor, 'heading-six');
break;
case 55:
event.preventDefault();
toggleBlock(editor, 'paragraph');
break;
case 56:
case KeyCode.Digit7:
case KeyCode.Digit8:
event.preventDefault();
toggleBlock(editor, 'paragraph');
break;
Expand Down Expand Up @@ -791,9 +815,6 @@
setAmountOfHeadersInSelection(Math.abs(anchor - focus) + 1);
}
}
} else {
setAmountOfHeadersInSelection(0);
setAmountOfHeadersInSelection(1);
}
}, 500);
}
Expand All @@ -813,6 +834,8 @@
activeStyleName !== '\u00A0'
) {
setActiveStyleName('\u00A0');
} else if (multipleStyles) {
setActiveStyleName('\u00A0');
}
return (
<li
Expand Down Expand Up @@ -909,6 +932,7 @@
)}
<div className={classes.editorWrapper}>
<Slate
key={valueKey}
editor={editor}
value={fragment}
onChange={(value) => {
Expand Down Expand Up @@ -1145,7 +1169,7 @@
},
dropdownButton: {
display: 'inline-flex',
maxWitdh: '100%',
maxWidth: '100%',
cursor: 'pointer',
backgroundColor: 'transparent',
padding: '4px',
Expand Down

0 comments on commit 81702c7

Please sign in to comment.