Add handler for justify text, fix dependency issue with auto highlights

This commit is contained in:
Jackson Harper
2023-04-06 22:52:31 +08:00
parent 94c0b9adaa
commit 14d5676979
6 changed files with 34 additions and 10 deletions

View File

@ -110,6 +110,7 @@ struct WebReader: PlatformViewRepresentable {
(webView as? OmnivoreWebView)?.updateLineHeight() (webView as? OmnivoreWebView)?.updateLineHeight()
(webView as? OmnivoreWebView)?.updateLabels(labelsJSON: item.labelsJSONString) (webView as? OmnivoreWebView)?.updateLabels(labelsJSON: item.labelsJSONString)
(webView as? OmnivoreWebView)?.updateTitle(title: item.title ?? "") (webView as? OmnivoreWebView)?.updateTitle(title: item.title ?? "")
(webView as? OmnivoreWebView)?.updateJustifyText()
webView.scrollView.indicatorStyle = ThemeManager.currentTheme.isDark ? webView.scrollView.indicatorStyle = ThemeManager.currentTheme.isDark ?
UIScrollView.IndicatorStyle.white : UIScrollView.IndicatorStyle.white :
UIScrollView.IndicatorStyle.black UIScrollView.IndicatorStyle.black

View File

@ -122,6 +122,16 @@ public final class OmnivoreWebView: WKWebView {
} }
} }
public func updateJustifyText() {
do {
if let justify = UserDefaults.standard.value(forKey: UserDefaultKey.justifyText.rawValue) as? Bool {
try dispatchEvent(.updateJustifyText(justify: justify))
}
} catch {
showErrorInSnackbar("Error updating justify-text")
}
}
public func updateTitle(title: String) { public func updateTitle(title: String) {
do { do {
try dispatchEvent(.updateTitle(title: title)) try dispatchEvent(.updateTitle(title: title))
@ -419,6 +429,7 @@ public enum WebViewDispatchEvent {
case updateColorMode(isDark: Bool) case updateColorMode(isDark: Bool)
case updateFontFamily(family: String) case updateFontFamily(family: String)
case updateTheme(themeName: String) case updateTheme(themeName: String)
case updateJustifyText(justify: Bool)
case saveAnnotation(annotation: String) case saveAnnotation(annotation: String)
case annotate case annotate
case highlight case highlight
@ -456,6 +467,8 @@ public enum WebViewDispatchEvent {
return "updateFontFamily" return "updateFontFamily"
case .updateTheme: case .updateTheme:
return "updateTheme" return "updateTheme"
case .updateJustifyText:
return "updateJustifyText"
case .saveAnnotation: case .saveAnnotation:
return "saveAnnotation" return "saveAnnotation"
case .annotate: case .annotate:
@ -499,6 +512,8 @@ public enum WebViewDispatchEvent {
return "event.themeName = '\(themeName)';" return "event.themeName = '\(themeName)';"
case let .updateFontSize(size: size): case let .updateFontSize(size: size):
return "event.fontSize = '\(size)';" return "event.fontSize = '\(size)';"
case let .updateJustifyText(justify: justify):
return "event.justifyText = \(justify);"
case let .updateColorMode(isDark: isDark): case let .updateColorMode(isDark: isDark):
return "event.isDark = '\(isDark)';" return "event.isDark = '\(isDark)';"
case let .updateFontFamily(family: family): case let .updateFontFamily(family: family):

File diff suppressed because one or more lines are too long

View File

@ -81,6 +81,7 @@ const App = () => {
lineHeight={window.lineHeight} lineHeight={window.lineHeight}
highlightOnRelease={window.highlightOnRelease} highlightOnRelease={window.highlightOnRelease}
highContrastText={window.prefersHighContrastFont ?? true} highContrastText={window.prefersHighContrastFont ?? true}
justifyText={window.justifyText}
articleMutations={{ articleMutations={{
createHighlightMutation: (input) => createHighlightMutation: (input) =>
mutation('createHighlight', input), mutation('createHighlight', input),

View File

@ -127,6 +127,7 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
const [highContrastText, setHighContrastText] = useState( const [highContrastText, setHighContrastText] = useState(
props.highContrastText ?? false props.highContrastText ?? false
) )
const [justifyText, setJustifyText] = useState(props.justifyText ?? false)
const highlightHref = useRef( const highlightHref = useRef(
window.location.hash ? window.location.hash.split('#')[1] : null window.location.hash ? window.location.hash.split('#')[1] : null
) )
@ -163,6 +164,7 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
const updateHighlightMode = (event: UpdateHighlightModeEvent) => { const updateHighlightMode = (event: UpdateHighlightModeEvent) => {
const isEnabled = event.enableHighlightOnRelease === 'on' const isEnabled = event.enableHighlightOnRelease === 'on'
console.log('setting highlight on release: ', isEnabled)
setHighlightOnRelease(isEnabled) setHighlightOnRelease(isEnabled)
} }
@ -228,6 +230,15 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
updateThemeLocally(isDark === 'true' ? ThemeId.Dark : ThemeId.Light) updateThemeLocally(isDark === 'true' ? ThemeId.Dark : ThemeId.Light)
} }
interface UpdateJustifyText extends Event {
justifyText?: boolean
}
const updateJustifyText = (event: UpdateJustifyText) => {
console.log('justify', event.justifyText)
setJustifyText(event.justifyText ?? false)
}
interface UpdateLabelsEvent extends Event { interface UpdateLabelsEvent extends Event {
labels?: Label[] labels?: Label[]
} }
@ -274,6 +285,8 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
'handleFontContrastChange', 'handleFontContrastChange',
handleFontContrastChange handleFontContrastChange
) )
document.addEventListener('updateJustifyText', updateJustifyText)
document.addEventListener('updateTitle', handleUpdateTitle) document.addEventListener('updateTitle', handleUpdateTitle)
document.addEventListener('updateLabels', handleUpdateLabels) document.addEventListener('updateLabels', handleUpdateLabels)
@ -297,6 +310,7 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
'handleFontContrastChange', 'handleFontContrastChange',
handleFontContrastChange handleFontContrastChange
) )
document.removeEventListener('updateJustifyText', updateJustifyText)
document.removeEventListener('updateTitle', handleUpdateTitle) document.removeEventListener('updateTitle', handleUpdateTitle)
document.removeEventListener('updateLabels', handleUpdateLabels) document.removeEventListener('updateLabels', handleUpdateLabels)
document.removeEventListener('share', share) document.removeEventListener('share', share)
@ -340,7 +354,7 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
maxWidth: `${styles.maxWidthPercentage ?? 100}%`, maxWidth: `${styles.maxWidthPercentage ?? 100}%`,
background: theme.colors.readerBg.toString(), background: theme.colors.readerBg.toString(),
'.article-inner-css': { '.article-inner-css': {
textAlign: props.justifyText ? 'justify' : 'start', textAlign: justifyText ? 'justify' : 'start',
}, },
'--text-font-family': styles.fontFamily, '--text-font-family': styles.fontFamily,
'--text-font-size': `${styles.fontSize}px`, '--text-font-size': `${styles.fontSize}px`,

View File

@ -61,13 +61,6 @@ export function useSelection(
let shouldCancelSelection = false let shouldCancelSelection = false
const overlapHighlights: HighlightLocation[] = [] const overlapHighlights: HighlightLocation[] = []
console.log(
'checking highlight locations for ',
selectionStart,
selectionEnd
)
console.log(' highlight locations: ', highlightLocations)
highlightLocations highlightLocations
.sort((a, b) => { .sort((a, b) => {
if (a.start < b.start) { if (a.start < b.start) {
@ -156,7 +149,7 @@ export function useSelection(
overlapHighlights: overlapHighlights.map(({ id }) => id), overlapHighlights: overlapHighlights.map(({ id }) => id),
}) })
}, },
[highlightLocations] [touchStartPos, selectionAttributes, highlightLocations]
) )
const copyTextSelection = useCallback(async () => { const copyTextSelection = useCallback(async () => {