Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F12142917
PostTextEditingView.swift
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Subscribers
None
PostTextEditingView.swift
View Options
import
SwiftUI
struct
PostTextEditingView
:
View
{
@
ObservedObject
var
post
:
WFAPost
@
Binding
var
updatingFromServer
:
Bool
@
State
private
var
appearance
:
PostAppearance
=
.
serif
@
State
private
var
combinedText
=
""
var
body
:
some
View
{
ZStack
(
alignment
:
.
topLeading
)
{
if
combinedText
.
count
==
0
{
Text
(
"Write…"
)
.
foregroundColor
(
Color
(
NSColor
.
placeholderTextColor
))
.
padding
(.
horizontal
,
5
)
.
font
(.
custom
(
appearance
.
rawValue
,
size
:
17
,
relativeTo
:
.
body
))
}
if
post
.
appearance
==
"sans"
{
MacEditorTextView
(
text
:
$
combinedText
,
isFirstResponder
:
combinedText
.
isEmpty
,
isEditable
:
true
,
font
:
NSFont
(
name
:
PostAppearance
.
sans
.
rawValue
,
size
:
17
),
onEditingChanged
:
onEditingChanged
,
onCommit
:
onCommit
,
onTextChange
:
onTextChange
)
}
else
if
post
.
appearance
==
"wrap"
||
post
.
appearance
==
"mono"
||
post
.
appearance
==
"code"
{
MacEditorTextView
(
text
:
$
combinedText
,
isFirstResponder
:
combinedText
.
isEmpty
,
isEditable
:
true
,
font
:
NSFont
(
name
:
PostAppearance
.
mono
.
rawValue
,
size
:
17
),
onEditingChanged
:
onEditingChanged
,
onCommit
:
onCommit
,
onTextChange
:
onTextChange
)
}
else
{
MacEditorTextView
(
text
:
$
combinedText
,
isFirstResponder
:
combinedText
.
isEmpty
,
isEditable
:
true
,
font
:
NSFont
(
name
:
PostAppearance
.
serif
.
rawValue
,
size
:
17
),
onEditingChanged
:
onEditingChanged
,
onCommit
:
onCommit
,
onTextChange
:
onTextChange
)
}
}
.
background
(
Color
(
NSColor
.
controlBackgroundColor
))
.
onAppear
(
perform
:
{
if
post
.
title
.
isEmpty
{
self
.
combinedText
=
post
.
body
}
else
{
self
.
combinedText
=
"#
\(
post
.
title
)
\n\n
\(
post
.
body
)
"
}
})
}
private
func
onEditingChanged
()
{
// Add code here to take action when the user first starts typing.
}
private
func
onTextChange
(
_
text
:
String
)
{
extractTitle
(
text
)
if
post
.
status
==
PostStatus
.
published
.
rawValue
&&
!
updatingFromServer
{
post
.
status
=
PostStatus
.
edited
.
rawValue
}
if
updatingFromServer
{
self
.
updatingFromServer
=
false
}
}
private
func
onCommit
()
{
// Add code here to take action when the user navigates away from the post.
}
private
func
extractTitle
(
_
text
:
String
)
{
var
detectedTitle
:
String
if
text
.
hasPrefix
(
"# "
)
{
let
endOfTitleIndex
=
text
.
firstIndex
(
of
:
"
\n
"
)
??
text
.
endIndex
detectedTitle
=
String
(
text
[..<
endOfTitleIndex
])
self
.
post
.
title
=
String
(
detectedTitle
.
dropFirst
(
"# "
.
count
))
let
remainingText
=
String
(
text
.
dropFirst
(
detectedTitle
.
count
).
dropFirst
(
1
))
if
remainingText
.
hasPrefix
(
"
\n
"
)
{
self
.
post
.
body
=
String
(
remainingText
.
dropFirst
(
1
))
}
else
{
self
.
post
.
body
=
remainingText
}
}
else
{
self
.
post
.
title
=
""
self
.
post
.
body
=
text
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Fri, Oct 10, 2:57 AM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3441035
Attached To
rWFSUI WriteFreely SwiftUI
Event Timeline
Log In to Comment