mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-22 11:03:21 -04:00
Merge pull request #1171 from kiwix/1170-remove-article-title-from-outline
1170 fix outlines and collapsibility
This commit is contained in:
commit
39b1f9d56e
@ -86,7 +86,7 @@ struct Language: Identifiable, Comparable {
|
||||
}
|
||||
}
|
||||
|
||||
class OutlineItem: ObservableObject, Identifiable {
|
||||
final class OutlineItem: ObservableObject, Identifiable {
|
||||
let id: String
|
||||
let index: Int
|
||||
let text: String
|
||||
|
@ -114,7 +114,7 @@ final class BrowserViewModel: NSObject, ObservableObject,
|
||||
// swiftlint:disable:next function_body_length
|
||||
@MainActor private init(tabID: NSManagedObjectID) {
|
||||
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, *) {
|
||||
webView.isInspectable = true
|
||||
}
|
||||
@ -732,14 +732,12 @@ final class BrowserViewModel: NSObject, ObservableObject,
|
||||
@MainActor private func generateOutlineTree(headings: [[String: String]]) {
|
||||
let root = OutlineItem(index: -1, text: "", level: 0)
|
||||
var stack: [OutlineItem] = [root]
|
||||
var all = [String: OutlineItem]()
|
||||
|
||||
headings.enumerated().forEach { index, heading in
|
||||
guard let id = heading["id"],
|
||||
let text = heading["text"],
|
||||
let tag = heading["tag"], let level = Int(tag.suffix(1)) else { return }
|
||||
let item = OutlineItem(id: id, index: index, text: text, level: level)
|
||||
all[item.id] = item
|
||||
|
||||
// get last item in stack
|
||||
// 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 let rootChildren = root.children, rootChildren.count == 1, let rootFirstChild = rootChildren.first {
|
||||
let children = rootFirstChild.removeAllChildren()
|
||||
self.outlineItemTree = [rootFirstChild] + children
|
||||
// if there is only one item at top level, with the same text as the article title
|
||||
// do not display it only it's children
|
||||
if let rootChildren = root.children, rootChildren.count == 1,
|
||||
let rootFirstChild = rootChildren.first,
|
||||
rootFirstChild.text.lowercased() == articleTitle.lowercased() {
|
||||
self.outlineItemTree = rootFirstChild.children ?? []
|
||||
} else {
|
||||
self.outlineItemTree = root.children ?? []
|
||||
}
|
||||
|
@ -226,10 +226,6 @@ extension WKWebView {
|
||||
}
|
||||
#endif
|
||||
|
||||
enum WebViewConfigCache {
|
||||
static let config = WebViewConfiguration()
|
||||
}
|
||||
|
||||
final class WebViewConfiguration: WKWebViewConfiguration {
|
||||
override init() {
|
||||
super.init()
|
||||
|
@ -19,14 +19,12 @@ struct OutlineButton: View {
|
||||
private let items: [OutlineItem]
|
||||
private let itemTree: [OutlineItem]
|
||||
private let scrollTo: (_ itemID: String) -> Void
|
||||
private let articleTitle: String
|
||||
@Environment(\.dismissSearch) private var dismissSearch
|
||||
@State private var isShowingOutline = false
|
||||
|
||||
init(browser: BrowserViewModel) {
|
||||
items = browser.outlineItems
|
||||
itemTree = browser.outlineItemTree
|
||||
articleTitle = browser.articleTitle
|
||||
scrollTo = { [weak browser] itemID in
|
||||
browser?.scrollTo(outlineItemID: itemID)
|
||||
}
|
||||
@ -69,7 +67,6 @@ struct OutlineButton: View {
|
||||
}.listStyle(.plain)
|
||||
}
|
||||
}
|
||||
.navigationTitle(articleTitle)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user