Merge pull request #1171 from kiwix/1170-remove-article-title-from-outline

1170 fix outlines and collapsibility
This commit is contained in:
Kelson 2025-04-26 21:31:55 +02:00 committed by GitHub
commit 39b1f9d56e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 8 additions and 15 deletions

View File

@ -86,7 +86,7 @@ struct Language: Identifiable, Comparable {
} }
} }
class OutlineItem: ObservableObject, Identifiable { final class OutlineItem: ObservableObject, Identifiable {
let id: String let id: String
let index: Int let index: Int
let text: String let text: String

View File

@ -114,7 +114,7 @@ final class BrowserViewModel: NSObject, ObservableObject,
// swiftlint:disable:next function_body_length // swiftlint:disable:next function_body_length
@MainActor private init(tabID: NSManagedObjectID) { @MainActor private init(tabID: NSManagedObjectID) {
self.tabID = tabID self.tabID = tabID
webView = WKWebView(frame: .zero, configuration: WebViewConfigCache.config) webView = WKWebView(frame: .zero, configuration: WebViewConfiguration())
if !Bundle.main.isProduction, #available(iOS 16.4, macOS 13.3, *) { if !Bundle.main.isProduction, #available(iOS 16.4, macOS 13.3, *) {
webView.isInspectable = true webView.isInspectable = true
} }
@ -732,14 +732,12 @@ final class BrowserViewModel: NSObject, ObservableObject,
@MainActor private func generateOutlineTree(headings: [[String: String]]) { @MainActor private func generateOutlineTree(headings: [[String: String]]) {
let root = OutlineItem(index: -1, text: "", level: 0) let root = OutlineItem(index: -1, text: "", level: 0)
var stack: [OutlineItem] = [root] var stack: [OutlineItem] = [root]
var all = [String: OutlineItem]()
headings.enumerated().forEach { index, heading in headings.enumerated().forEach { index, heading in
guard let id = heading["id"], guard let id = heading["id"],
let text = heading["text"], let text = heading["text"],
let tag = heading["tag"], let level = Int(tag.suffix(1)) else { return } let tag = heading["tag"], let level = Int(tag.suffix(1)) else { return }
let item = OutlineItem(id: id, index: index, text: text, level: level) let item = OutlineItem(id: id, index: index, text: text, level: level)
all[item.id] = item
// get last item in stack // get last item in stack
// if last item is child of item's sibling, unwind stack until a sibling is found // if last item is child of item's sibling, unwind stack until a sibling is found
@ -760,10 +758,12 @@ final class BrowserViewModel: NSObject, ObservableObject,
} }
} }
// if there is only one h1, flatten one level // if there is only one item at top level, with the same text as the article title
if let rootChildren = root.children, rootChildren.count == 1, let rootFirstChild = rootChildren.first { // do not display it only it's children
let children = rootFirstChild.removeAllChildren() if let rootChildren = root.children, rootChildren.count == 1,
self.outlineItemTree = [rootFirstChild] + children let rootFirstChild = rootChildren.first,
rootFirstChild.text.lowercased() == articleTitle.lowercased() {
self.outlineItemTree = rootFirstChild.children ?? []
} else { } else {
self.outlineItemTree = root.children ?? [] self.outlineItemTree = root.children ?? []
} }

View File

@ -226,10 +226,6 @@ extension WKWebView {
} }
#endif #endif
enum WebViewConfigCache {
static let config = WebViewConfiguration()
}
final class WebViewConfiguration: WKWebViewConfiguration { final class WebViewConfiguration: WKWebViewConfiguration {
override init() { override init() {
super.init() super.init()

View File

@ -19,14 +19,12 @@ struct OutlineButton: View {
private let items: [OutlineItem] private let items: [OutlineItem]
private let itemTree: [OutlineItem] private let itemTree: [OutlineItem]
private let scrollTo: (_ itemID: String) -> Void private let scrollTo: (_ itemID: String) -> Void
private let articleTitle: String
@Environment(\.dismissSearch) private var dismissSearch @Environment(\.dismissSearch) private var dismissSearch
@State private var isShowingOutline = false @State private var isShowingOutline = false
init(browser: BrowserViewModel) { init(browser: BrowserViewModel) {
items = browser.outlineItems items = browser.outlineItems
itemTree = browser.outlineItemTree itemTree = browser.outlineItemTree
articleTitle = browser.articleTitle
scrollTo = { [weak browser] itemID in scrollTo = { [weak browser] itemID in
browser?.scrollTo(outlineItemID: itemID) browser?.scrollTo(outlineItemID: itemID)
} }
@ -69,7 +67,6 @@ struct OutlineButton: View {
}.listStyle(.plain) }.listStyle(.plain)
} }
} }
.navigationTitle(articleTitle)
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.toolbar { .toolbar {
ToolbarItem(placement: .cancellationAction) { ToolbarItem(placement: .cancellationAction) {