Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class EnrichedTextInputView : AppCompatEditText {

val mentionHandler: MentionHandler? = MentionHandler(this)
var htmlStyle: HtmlStyle = HtmlStyle(this, null)
set(value) {
if (field != value) {
field = value
reapplyTextWithNewStyles()
}
}
var spanWatcher: EnrichedSpanWatcher? = null
var layoutManager: EnrichedTextInputViewLayoutManager = EnrichedTextInputViewLayoutManager(this)

Expand Down Expand Up @@ -533,6 +539,27 @@ class EnrichedTextInputView : AppCompatEditText {
}
}

private fun reapplyTextWithNewStyles() {
val currentText = text
if (currentText != null && currentText.isNotEmpty()) {
runAsATransaction {
val currentHtml = EnrichedParser.toHtml(currentText as Spannable)
val newText = parseText(currentHtml)

val currentSelectionStart = selectionStart
val currentSelectionEnd = selectionEnd

setText(newText)

val newLength = text?.length ?: 0
val safeStart = currentSelectionStart.coerceIn(0, newLength)
val safeEnd = currentSelectionEnd.coerceIn(0, newLength)
setSelection(safeStart, safeEnd)
layoutManager.invalidateLayout()
}
}
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()

Expand Down
77 changes: 76 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export default function App() {
const [isLinkModalOpen, setIsLinkModalOpen] = useState(false);
const [isValueModalOpen, setIsValueModalOpen] = useState(false);
const [currentHtml, setCurrentHtml] = useState('');
const [currentHtmlStyle, setCurrentHtmlStyle] =
useState<HtmlStyle>(htmlStyle);

const [selection, setSelection] = useState<Selection>();
const [stylesState, setStylesState] = useState<StylesState>(DEFAULT_STYLE);
Expand Down Expand Up @@ -235,6 +237,12 @@ export default function App() {
closeChannelMentionPopup();
};

const handleChangeHtmlStyle = () => {
setCurrentHtmlStyle((style) =>
style === htmlStyle ? anotherHtmlStyle : htmlStyle
);
};

const handleFocusEvent = () => {
console.log('Input focused');
};
Expand Down Expand Up @@ -267,7 +275,7 @@ export default function App() {
key={key}
mentionIndicators={['@', '#']}
style={styles.editorInput}
htmlStyle={htmlStyle}
htmlStyle={currentHtmlStyle}
placeholder="Type something here..."
placeholderTextColor="rgb(0, 26, 114)"
selectionColor="deepskyblue"
Expand Down Expand Up @@ -307,6 +315,11 @@ export default function App() {
/>
<Button title="GC" onPress={collectGarbage} style={styles.button} />
</View>
<Button
title="Change HTML Style"
onPress={handleChangeHtmlStyle}
style={styles.valueButton}
/>
<Button
title="Set input's value"
onPress={openValueModal}
Expand Down Expand Up @@ -345,6 +358,68 @@ export default function App() {
);
}

const anotherHtmlStyle: HtmlStyle = {
h1: {
fontSize: 100,
bold: false,
},
h2: {
fontSize: 50,
bold: false,
},
h3: {
fontSize: 30,
bold: false,
},
blockquote: {
borderColor: 'orange',
borderWidth: 1,
gapWidth: 4,
color: 'orange',
},
codeblock: {
color: 'red',
borderRadius: 0,
backgroundColor: 'black',
},
code: {
color: 'yellow',
backgroundColor: 'purple',
},
a: {
color: 'red',
textDecorationLine: 'none',
},
mention: {
'#': {
color: 'darkred',
backgroundColor: 'lightcoral',
textDecorationLine: 'none',
},
'@': {
color: 'darkgreen',
backgroundColor: 'transparent',
textDecorationLine: 'underline',
},
},
img: {
width: 200,
height: 200,
},
ol: {
gapWidth: 4,
marginLeft: 8,
markerColor: 'orange',
markerFontWeight: 'normal',
},
ul: {
bulletColor: 'black',
bulletSize: 2,
marginLeft: 8,
gapWidth: 4,
},
};

const htmlStyle: HtmlStyle = {
h1: {
fontSize: 40,
Expand Down