Fix saving vertical orientation flag (fix #1653)

This commit is contained in:
1024jp 2024-06-12 19:16:34 +09:00
parent 1955530bd5
commit 6ce97be7f6
3 changed files with 21 additions and 7 deletions

View File

@ -12,6 +12,7 @@
### Fixes
- Fix an issue that a saved document could not revert to the horizontal text orientation when it was saved once as a vertical text document.
- Fix a trivial memory leak in the line ending menu (thanks to Yoshimasa Niwa).

View File

@ -452,6 +452,11 @@ final class Document: NSDocument, AdditionalDocumentPreparing, EncodingChanging
try super.writeSafely(to: url, ofType: typeName, for: saveOperation)
if saveOperation != .autosaveElsewhereOperation {
// set/remove flag for vertical text orientation
if UserDefaults.standard[.savesTextOrientation] {
try? url.setExtendedAttribute(data: self.isVerticalText ? Data([1]) : nil, for: FileExtendedAttributeName.verticalText)
}
// get the latest file attributes
do {
let attributes = try FileManager.default.attributesOfItem(atPath: url.path) // FILE_ACCESS
@ -480,6 +485,8 @@ final class Document: NSDocument, AdditionalDocumentPreparing, EncodingChanging
}
// save document state to the extended file attributes
// -> Save FileExtendedAttributeName.verticalText at `super.writeSafely(to:ofType:for:)`
// since the xattr already set to the file cannot remove at this point. (2024-06-12)
var xattrs: [String: Data] = [:]
if self.shouldSaveEncodingXattr {
xattrs[FileExtendedAttributeName.encoding] = self.fileEncoding.encoding.xattrEncodingData
@ -487,9 +494,6 @@ final class Document: NSDocument, AdditionalDocumentPreparing, EncodingChanging
if self.suppressesInconsistentLineEndingAlert {
xattrs[FileExtendedAttributeName.allowLineEndingInconsistency] = Data([1])
}
if UserDefaults.standard[.savesTextOrientation] {
xattrs[FileExtendedAttributeName.verticalText] = self.isVerticalText ? Data([1]) : nil
}
if !xattrs.isEmpty {
attributes[FileAttributeKey.extendedAttributes.rawValue] = xattrs
}

View File

@ -8,7 +8,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2018-2023 1024jp
// © 2018-2024 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -27,7 +27,10 @@ import Foundation
extension URL {
/// Gets extended attribute
/// Gets extended attribute.
///
/// - Parameter name: The key name of the attribute to get.
/// - Returns: Data.
func extendedAttribute(for name: String) throws -> Data {
try self.withUnsafeFileSystemRepresentation { fileSystemPath -> Data in
@ -49,7 +52,11 @@ extension URL {
}
/// Sets extended attribute
/// Sets extended attribute.
///
/// - Parameters:
/// - data: The data to set.
/// - name: The attribute key name to set.
func setExtendedAttribute(data: Data?, for name: String) throws {
// remove if nil is passed
@ -67,7 +74,9 @@ extension URL {
}
/// Removes extended attribute
/// Removes extended attribute.
///
/// - Parameter name: The attribute key name to remove.
private func removeExtendedAttribute(for name: String) throws {
try self.withUnsafeFileSystemRepresentation { fileSystemPath in