Add basic document editing

Cette révision appartient à :
Shadowfacts 2020-04-06 19:31:26 -04:00
Parent 33b1159848
révision c89d6d5f39
Signé par: shadowfacts
ID de la clé GPG: 94A5AB95422746E5
8 fichiers modifiés avec 309 ajouts et 3 suppressions

Voir le fichier

@ -13,6 +13,10 @@
D60C864023CA2E2100C9DB8E /* ServerConnectViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */; };
D62408C12438CF550020E09F /* JavaScriptEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D62408C02438CF550020E09F /* JavaScriptEditorView.swift */; };
D624090F243903E90020E09F /* ExtendedJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = D624090E243903E90020E09F /* ExtendedJSON.swift */; };
D626BF82243BD2EE0075117B /* EditDocumentWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D626BF80243BD2EE0075117B /* EditDocumentWindowController.swift */; };
D626BF83243BD2EE0075117B /* EditDocumentWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D626BF81243BD2EE0075117B /* EditDocumentWindowController.xib */; };
D626BF86243BE19A0075117B /* EditDocumentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D626BF84243BE19A0075117B /* EditDocumentViewController.swift */; };
D626BF87243BE19A0075117B /* EditDocumentViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D626BF85243BE19A0075117B /* EditDocumentViewController.xib */; };
D63CDEBE23C837DC0012D658 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDEBD23C837DC0012D658 /* AppDelegate.swift */; };
D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEBF23C837DD0012D658 /* Assets.xcassets */; };
D63CDEC323C837DD0012D658 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEC123C837DD0012D658 /* MainMenu.xib */; };
@ -88,6 +92,10 @@
D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectViewController.xib; sourceTree = "<group>"; };
D62408C02438CF550020E09F /* JavaScriptEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JavaScriptEditorView.swift; sourceTree = "<group>"; };
D624090E243903E90020E09F /* ExtendedJSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtendedJSON.swift; sourceTree = "<group>"; };
D626BF80243BD2EE0075117B /* EditDocumentWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditDocumentWindowController.swift; sourceTree = "<group>"; };
D626BF81243BD2EE0075117B /* EditDocumentWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = EditDocumentWindowController.xib; sourceTree = "<group>"; };
D626BF84243BE19A0075117B /* EditDocumentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditDocumentViewController.swift; sourceTree = "<group>"; };
D626BF85243BE19A0075117B /* EditDocumentViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = EditDocumentViewController.xib; sourceTree = "<group>"; };
D63CDEBA23C837DC0012D658 /* MongoView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MongoView.app; sourceTree = BUILT_PRODUCTS_DIR; };
D63CDEBD23C837DC0012D658 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D63CDEBF23C837DD0012D658 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -153,6 +161,8 @@
D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */,
D63CDF3A23C838470012D658 /* DatabaseWindowController.swift */,
D63CDF3B23C838470012D658 /* DatabaseWindowController.xib */,
D626BF80243BD2EE0075117B /* EditDocumentWindowController.swift */,
D626BF81243BD2EE0075117B /* EditDocumentWindowController.xib */,
);
path = Windows;
sourceTree = "<group>";
@ -166,6 +176,8 @@
D63CDF4323C970C50012D658 /* DatabaseViewController.xib */,
D63CDF3E23C839010012D658 /* QueryViewController.swift */,
D63CDF3F23C839010012D658 /* QueryViewController.xib */,
D626BF84243BE19A0075117B /* EditDocumentViewController.swift */,
D626BF85243BE19A0075117B /* EditDocumentViewController.xib */,
);
path = "View Controllers";
sourceTree = "<group>";
@ -334,10 +346,12 @@
D60C864023CA2E2100C9DB8E /* ServerConnectViewController.xib in Resources */,
D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */,
D60C863A23CA2DD100C9DB8E /* ServerConnectWindowController.xib in Resources */,
D626BF87243BE19A0075117B /* EditDocumentViewController.xib in Resources */,
D63CDF4523C970C50012D658 /* DatabaseViewController.xib in Resources */,
D6A7D09A243546B500B46857 /* WindowStatusView.xib in Resources */,
D63CDF3D23C838470012D658 /* DatabaseWindowController.xib in Resources */,
D63CDEC323C837DD0012D658 /* MainMenu.xib in Resources */,
D626BF83243BD2EE0075117B /* EditDocumentWindowController.xib in Resources */,
D63CDF4123C839010012D658 /* QueryViewController.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -355,10 +369,12 @@
D63CDF3823C8381A0012D658 /* MongoController.swift in Sources */,
D60C863923CA2DD100C9DB8E /* ServerConnectWindowController.swift in Sources */,
D63CDF3723C8381A0012D658 /* Node.swift in Sources */,
D626BF86243BE19A0075117B /* EditDocumentViewController.swift in Sources */,
D60C863F23CA2E2100C9DB8E /* ServerConnectViewController.swift in Sources */,
D63CDF4423C970C50012D658 /* DatabaseViewController.swift in Sources */,
D63CDF3C23C838470012D658 /* DatabaseWindowController.swift in Sources */,
D6A7D096243541A400B46857 /* WindowStatusView.swift in Sources */,
D626BF82243BD2EE0075117B /* EditDocumentWindowController.swift in Sources */,
D6A7D0A42435885B00B46857 /* JavaScriptHighlighter.swift in Sources */,
D62408C12438CF550020E09F /* JavaScriptEditorView.swift in Sources */,
D63CDF4023C839010012D658 /* QueryViewController.swift in Sources */,

Voir le fichier

@ -8,6 +8,7 @@
import Foundation
import JavaScriptCore
import MongoSwift
struct ExtendedJSON {
private init() {}
@ -25,4 +26,27 @@ struct ExtendedJSON {
return context.evaluateScript("JSON.stringify(\(string))")?.toString()
}
private static func fromExtJSON(_ string: String) -> Document? {
do {
let doc = try Document(fromJSON: string)
return doc
} catch {
print("Unable to create document from extended JSON: \(error)")
return nil
}
}
static func toDocument(_ string: String) -> Document? {
guard let normalized = normalize(string),
let doc = fromExtJSON(normalized) else {
return nil
}
return doc
}
static func prettify(_ string: String) -> String? {
let command = "JSON.stringify(JSON.parse(`\(string)`), null, 4)"
return context.evaluateScript(command)?.toString()
}
}

Voir le fichier

@ -0,0 +1,70 @@
//
// EditDocumentViewController.swift
// MongoView
//
// Created by Shadowfacts on 4/6/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import Cocoa
import MongoSwift
class EditDocumentViewController: NSViewController {
private(set) var mongoController: MongoController!
private(set) var collection: DatabaseCollection!
private(set) var document: Document!
var documentEdited: (() -> Void)?
@IBOutlet var editorTextView: JavaScriptEditorView!
@IBOutlet weak var saveButton: NSButton!
@IBOutlet weak var cancelButton: NSButton!
@IBOutlet weak var validateButton: NSButton!
init(mongoController: MongoController, collection: DatabaseCollection, document: Document) {
self.mongoController = mongoController
self.collection = collection
self.document = document
super.init(nibName: "EditDocumentViewController", bundle: .main)
}
required init?(coder: NSCoder) {
fatalError()
}
override func viewDidLoad() {
super.viewDidLoad()
editorTextView.string = ExtendedJSON.prettify(document.extendedJSON) ?? document.extendedJSON
editorTextView.isAutomaticQuoteSubstitutionEnabled = false
}
@IBAction func savePressed(_ sender: Any) {
guard let id = document["_id"],
let newDoc = ExtendedJSON.toDocument(editorTextView.string) else {
// todo: error
return
}
let collection = mongoController.collection(self.collection)
collection.replaceOne(filter: ["_id": id], replacement: newDoc).whenComplete { (result) in
DispatchQueue.main.async {
switch result {
case .success(_):
self.documentEdited?()
case let .failure(error):
print("Unable to replace document: \(error)")
}
self.view.window!.close()
}
}
}
@IBAction func cancelPressed(_ sender: Any) {
view.window!.close()
}
@IBAction func validatePressed(_ sender: Any) {
}
}

Voir le fichier

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16096" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16096"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="EditDocumentViewController" customModule="MongoView" customModuleProvider="target">
<connections>
<outlet property="cancelButton" destination="5M7-Eo-LRQ" id="9iE-dG-Rvz"/>
<outlet property="editorTextView" destination="1aa-Vo-yPS" id="hip-Qi-9BH"/>
<outlet property="saveButton" destination="3aO-fI-F6G" id="hbc-f1-hAJ"/>
<outlet property="validateButton" destination="J9n-en-a5p" id="SoQ-HS-byg"/>
<outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="Hz6-mo-xeY">
<rect key="frame" x="0.0" y="0.0" width="480" height="272"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<stackView distribution="fill" orientation="horizontal" alignment="top" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="xa0-UE-ywz">
<rect key="frame" x="273" y="8" width="199" height="21"/>
<subviews>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="5M7-Eo-LRQ">
<rect key="frame" x="-6" y="-7" width="82" height="32"/>
<buttonCell key="cell" type="push" title="Cancel" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="xeo-Kh-gMm">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
Gw
</string>
</buttonCell>
<connections>
<action selector="cancelPressed:" target="-2" id="bmf-UI-VmZ"/>
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="3aO-fI-F6G">
<rect key="frame" x="72" y="-7" width="133" height="32"/>
<buttonCell key="cell" type="push" title="Save and Close" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="FgS-jM-BWn">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="savePressed:" target="-2" id="Blr-6P-WPb"/>
</connections>
</button>
</subviews>
<visibilityPriorities>
<integer value="1000"/>
<integer value="1000"/>
</visibilityPriorities>
<customSpacing>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
</customSpacing>
</stackView>
<scrollView borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MlX-0S-w82">
<rect key="frame" x="8" y="37" width="464" height="227"/>
<clipView key="contentView" drawsBackground="NO" copiesOnScroll="NO" id="egN-c7-XBK">
<rect key="frame" x="0.0" y="0.0" width="449" height="227"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView importsGraphics="NO" richText="NO" verticallyResizable="YES" id="1aa-Vo-yPS" customClass="JavaScriptEditorView" customModule="MongoView" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="449" height="227"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<size key="minSize" width="449" height="227"/>
<size key="maxSize" width="465" height="10000000"/>
<color key="insertionPointColor" name="textColor" catalog="System" colorSpace="catalog"/>
</textView>
</subviews>
</clipView>
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="YES" id="IwO-eE-cem">
<rect key="frame" x="-100" y="-100" width="225" height="15"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" verticalHuggingPriority="750" horizontal="NO" id="IDr-mZ-kqd">
<rect key="frame" x="449" y="0.0" width="15" height="227"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="J9n-en-a5p">
<rect key="frame" x="2" y="1" width="89" height="32"/>
<buttonCell key="cell" type="push" title="Validate" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="J6T-UY-zPK">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="validatePressed:" target="-2" id="gVI-wh-rT2"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="MlX-0S-w82" firstAttribute="top" secondItem="Hz6-mo-xeY" secondAttribute="top" constant="8" id="BdG-le-KXB"/>
<constraint firstItem="J9n-en-a5p" firstAttribute="top" secondItem="MlX-0S-w82" secondAttribute="bottom" constant="8" id="FvY-Be-Tdx"/>
<constraint firstItem="J9n-en-a5p" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" constant="8" id="HzC-6Y-gZG"/>
<constraint firstAttribute="bottom" secondItem="xa0-UE-ywz" secondAttribute="bottom" constant="8" id="Ssc-Oa-bTe"/>
<constraint firstItem="xa0-UE-ywz" firstAttribute="top" secondItem="MlX-0S-w82" secondAttribute="bottom" constant="8" id="Uga-ZA-1b4"/>
<constraint firstAttribute="trailing" secondItem="xa0-UE-ywz" secondAttribute="trailing" constant="8" id="i4O-th-zRP"/>
<constraint firstAttribute="bottom" secondItem="J9n-en-a5p" secondAttribute="bottom" constant="8" id="lZs-Qs-8gq"/>
<constraint firstItem="MlX-0S-w82" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" constant="8" id="m99-4c-vcx"/>
<constraint firstAttribute="trailing" secondItem="MlX-0S-w82" secondAttribute="trailing" constant="8" id="rYV-TB-qaV"/>
</constraints>
<point key="canvasLocation" x="140" y="154"/>
</customView>
</objects>
</document>

Voir le fichier

@ -79,8 +79,7 @@ class QueryViewController: NSViewController {
let filterText = filterTextView.string.trimmingCharacters(in: .whitespacesAndNewlines)
let filter: Document
if !filterText.isEmpty,
let normalized = ExtendedJSON.normalize(filterText),
let doc = try? Document(fromJSON: normalized) {
let doc = ExtendedJSON.toDocument(filterText) {
filter = doc
} else {
filter = [:]
@ -162,11 +161,24 @@ class QueryViewController: NSViewController {
}
}
@IBAction func editDocument(_ sender: Any) {
guard let node = outlineView.item(atRow: outlineView.clickedRow) as? Node,
node.parent == nil,
case let .document(document) = node.value else {
return
}
let wc = EditDocumentWindowController(mongoController: mongoController, collection: collection, document: document)
wc.documentEdited = {
self.refresh()
}
wc.showWindow(nil)
}
}
extension QueryViewController: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
if menuItem.action == #selector(deleteNode(_:)) {
if menuItem.action == #selector(deleteNode(_:)) || menuItem.action == #selector(editDocument(_:)) {
if outlineView.clickedRow != -1, let node = outlineView.item(atRow: outlineView.clickedRow) as? Node, node.parent == nil {
return true
} else {

Voir le fichier

@ -1139,6 +1139,12 @@
</customView>
<menu id="nL5-kg-dty">
<items>
<menuItem title="Edit Document" id="0gS-XH-YDt">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="editDocument:" target="-2" id="DP4-Tq-o5M"/>
</connections>
</menuItem>
<menuItem title="Delete" id="nn0-ZF-eDZ">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>

Voir le fichier

@ -0,0 +1,38 @@
//
// EditDocumentWindowController.swift
// MongoView
//
// Created by Shadowfacts on 4/6/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import Cocoa
import MongoSwift
class EditDocumentWindowController: NSWindowController {
private(set) var mongoController: MongoController!
private(set) var collection: DatabaseCollection!
private(set) var mongoDocument: Document!
var documentEdited: (() -> Void)?
convenience init(mongoController: MongoController, collection: DatabaseCollection, document: Document) {
self.init(windowNibName: "EditDocumentWindowController")
self.mongoController = mongoController
self.collection = collection
self.mongoDocument = document
}
override func windowDidLoad() {
super.windowDidLoad()
window!.title = "Edit \(collection.database).\(collection.name) Document"
let vc = EditDocumentViewController(mongoController: mongoController, collection: collection, document: mongoDocument)
vc.documentEdited = documentEdited
contentViewController = vc
}
}

Voir le fichier

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16096" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16096"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="EditDocumentWindowController" customModule="MongoView" customModuleProvider="target">
<connections>
<outlet property="window" destination="F0z-JX-Cv5" id="gIp-Ho-8D9"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="documentWindow" id="F0z-JX-Cv5">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<rect key="contentRect" x="931" y="526" width="1200" height="900"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/>
<value key="minSize" type="size" width="900" height="600"/>
<view key="contentView" id="se5-gp-TjO">
<rect key="frame" x="0.0" y="0.0" width="1200" height="900"/>
<autoresizingMask key="autoresizingMask"/>
</view>
<connections>
<outlet property="delegate" destination="-2" id="0bl-1N-AYu"/>
</connections>
<point key="canvasLocation" x="140" y="147"/>
</window>
</objects>
</document>