forked from itsjustcon/JavaScriptCoreBrowserObjectModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentBrowserViewController.swift
More file actions
193 lines (155 loc) · 8.36 KB
/
DocumentBrowserViewController.swift
File metadata and controls
193 lines (155 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// DocumentBrowserViewController.swift
// JS Notepad
//
// Created by Connor Grady on 1/6/18.
// Copyright © 2018 Connor Grady. All rights reserved.
//
import UIKit
class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
//override var allowedContentTypes: [String] {
// return ["com.netscape.javascript-source"]
// //return [Document().fileType]
//}
/*
override init(forOpeningFilesWithContentTypes allowedContentTypes: [String]?) {
print("DocumentBrowserViewController init( forOpeningFilesWithContentTypes: \(String(describing: allowedContentTypes)) )")
super.init(forOpeningFilesWithContentTypes: allowedContentTypes)
}
required init?(coder aDecoder: NSCoder) {
print("DocumentBrowserViewController init?( coder: NSCoder )")
super.init(coder: aDecoder)
print(" allowedContentTypes: \(allowedContentTypes)")
}
*/
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = true
allowsPickingMultipleItems = false
// Update the style of the UIDocumentBrowserViewController
browserUserInterfaceStyle = .dark
view.tintColor = .white
// Specify the allowed content types of your application via the Info.plist.
// Do any additional setup after loading the view, typically from a nib.
}
// MARK: UIDocumentBrowserViewControllerDelegate
func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
print("DocumentBrowserViewController documentBrowser( controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode ) )")
/*
let newDocumentURL: URL? = nil
// Set the URL for the new document here. Optionally, you can present a template chooser before calling the importHandler.
// Make sure the importHandler is always called, even if the user cancels the creation request.
if newDocumentURL != nil {
importHandler(newDocumentURL, .move)
} else {
importHandler(nil, .none)
}
*/
// Get a temporary URL...
//let documentDirectory = FileManager.default.temporaryDirectory
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
//let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let url = documentDirectory.absoluteURL.appendingPathComponent("\(UUID().uuidString).js", isDirectory: false)
print(" url: \(url)")
let doc = JSDocument(fileURL: url)
/*
// Create a new document in a temporary location
doc.save(to: url, for: .forCreating) { (saveSuccess) in
// Make sure the document saved successfully
guard saveSuccess else {
// Cancel document creation
importHandler(nil, .none)
return
}
// Close the document.
doc.close(completionHandler: { (closeSuccess) in
// Make sure the document closed successfully
guard closeSuccess else {
// Cancel document creation
importHandler(nil, .none)
return
}
// Pass the document's temporary URL to the import handler.
importHandler(url, .move)
})
}
*/
FileManager.default.createFile(atPath: url.path, contents: nil, attributes: [:])
//doc.save(to: url, for: .forCreating, completionHandler: { success in
// if success {
// importHandler(url, .move)
// } else {
// importHandler(nil, .none)
// }
//})
doc.save(to: url, for: .forCreating) { (saveSuccess) in
// Make sure the document saved successfully
guard saveSuccess else {
// Cancel document creation
importHandler(nil, .none)
return
}
// Close the document.
doc.close(completionHandler: { (closeSuccess) in
// Make sure the document closed successfully
guard closeSuccess else {
// Cancel document creation
importHandler(nil, .none)
return
}
// Pass the document's temporary URL to the import handler.
importHandler(url, .move)
})
}
/*
// `Untitled-X.js` support
var idx = 0
var fileurl = documentDirectory.absoluteURL.appendingPathComponent("Untitled.js", isDirectory: false)
print(" fileurl: \(fileurl)")
while FileManager.default.fileExists(atPath: fileurl.path) {
idx = idx + 1
fileurl = fileurl.deletingLastPathComponent().appendingPathComponent("Untitled-\(idx).js", isDirectory: false)
print(" fileurl: \(fileurl)")
}
FileManager.default.createFile(atPath: fileurl.path, contents: nil, attributes: nil)
doc.save(to: fileurl, for: .forCreating, completionHandler: { success in
if success {
importHandler(fileurl, .move)
} else {
importHandler(nil, .none)
}
})
*/
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
print("DocumentBrowserViewController documentBrowser( controller: UIDocumentBrowserViewController, didPickDocumentURLs: \(documentURLs) )")
guard let sourceURL = documentURLs.first else { return }
// Present the Document View Controller for the first document that was picked.
// If you support picking multiple items, make sure you handle them all.
presentDocument(at: sourceURL)
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
print("DocumentBrowserViewController documentBrowser( controller: UIDocumentBrowserViewController, didImportDocumentAt: \(sourceURL), toDestinationURL: \(destinationURL) )")
// Present the Document View Controller for the new newly created document
presentDocument(at: destinationURL)
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {
print("DocumentBrowserViewController documentBrowser( controller: UIDocumentBrowserViewController, failedToImportDocumentAt: \(documentURL), error: \(String(describing: error)) )")
// Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
}
// MARK: Document Presentation
func presentDocument(at documentURL: URL) {
print("DocumentBrowserViewController presentDocument( at: \(documentURL) )")
//let storyBoard = UIStoryboard(name: "Main", bundle: nil)
//let documentViewController = storyBoard.instantiateViewController(withIdentifier: "DocumentViewController") as! DocumentViewController
//documentViewController.document = Document(fileURL: documentURL)
//present(documentViewController, animated: true, completion: nil)
//let navigationController = UINavigationController(rootViewController: documentViewController)
let navigationController = storyboard!.instantiateViewController(withIdentifier: "DocumentNavigationController") as! UINavigationController
let documentViewController = navigationController.viewControllers.first as! JSDocumentViewController
documentViewController.document = JSDocument(fileURL: documentURL)
present(navigationController, animated: true, completion: nil)
//performSegue(withIdentifier: "showDocument", sender: self)
}
}