danielsaidi / RichTextKit

RichTextKit is a Swift SDK that helps you use rich text in Swift and SwiftUI.
MIT License
944 stars 126 forks source link

Text kit 2 bug background #211

Open kissmaxvic opened 3 weeks ago

kissmaxvic commented 3 weeks ago

When I show and close the RichTextEditor view several times, an error appears: the background becomes white or black (depending on the device’s theme).

There’s also always an error indicating that text kit 1 is being used, and in that case, everything works fine (no background errors), but as soon as the error disappears, the background turns white or black.

The error under which everything works, funny as it may sound:

Снимок экрана 2024-10-31 в 05 02 01

Display: ScreenRecording_10-31-2024-06-15-16_1

Code: VStack { FcomFctm(stuck: stuck) TextEditorKit(text: stuck.textRussian, height: stuck.textRusHeight, rusOrNo: true)

         if stuck.textOriginBool.wrappedValue {
                 TextEditorKit(text: stuck.textOrigin, height: stuck.textOriginHeight, rusOrNo: false)
          }

}

import SwiftUI import RichTextKit

struct TextEditorKit: View {

@StateObject var context: RichTextContext = RichTextContext()

@Binding var text: NSAttributedString

// height of texteditro
@Binding var height: Double
@State var rusOrNo: Bool

@State private var isInspectorPresented = false

@State var dragEnded: Double = 0.0

var body: some View {
    VStack(spacing: 0) {
        ZStack {
            RoundedRectangle(cornerRadius: 20)
                .foregroundColor(rusOrNo ? .textBackground : .textBackgroundYellow)

if os(macOS)

            RichTextFormat.Toolbar(context: context)

endif

            RichTextEditor(
                text: $text,
                context: context
            )
            {
                if $0.richText.string.isEmpty {
                    if rusOrNo {
                        $0.pasteText("Текст", at: 0)
                    } else {
                        $0.pasteText("Text", at: 0)
                    }
                    $0.textContentInset = CGSize(width: 10, height: 10)
                    $0.setRichTextColor(.foreground, to: .white, at: NSRange(location: 0, length: 5))
                }
            }

            .richTextEditorConfig(
                RichTextEditorConfig(isScrollingEnabled: true)
            )
            .richTextEditorStyle(RichTextEditorStyle(fontColor: .white))

            // Use this to just view the text:
            // RichTextViewer(document.text)

if os(iOS)

            //            RichTextKeyboardToolbar(
            //                context: context,
            //                leadingButtons: { $0 },
            //                trailingButtons: { $0 },
            //                formatSheet: { $0 }
            //            )

endif

        }
        .frame(height: 35 + height)
        .toolbar {
            ToolbarItem(placement: .automatic) {
                Toggle(isOn: $isInspectorPresented) {
                    Image.richTextFormatBrush
                        .resizable()
                        .aspectRatio(1, contentMode: .fit)
                }
            }
        }

        .focusedValue(\.richTextContext, context)
        .richTextFormatSheetConfig(.init(colorPickers: colorPickers))
        .richTextFormatSidebarConfig(
            .init(
                colorPickers: colorPickers,
                fontPicker: isMac
            )
        )
        .richTextFormatToolbarConfig(.init(colorPickers: []))
        .viewDebug()
        ZStack {
            Path { path in
                path.move(to: CGPoint(x: -50, y: 0))
                path.addCurve(to: CGPoint(x: 0, y: 20), control1: CGPoint(x: -20, y: 2), control2: CGPoint(x: -30, y: 20))
                path.addCurve(to: CGPoint(x: 50, y: 0), control1: CGPoint(x: 30, y: 20), control2: CGPoint(x: 20, y: 2))
            }
            .foregroundColor(rusOrNo ? .textBackground : .textBackgroundYellow)
            .frame(width: 1)
            Image(systemName: "arrow.up.and.down")
                .resizable()
                .foregroundColor(.white)
                .frame(width: 5, height: 10)
        }
        .frame(width: 50)
        .gesture(
            DragGesture()
                .onChanged { gesture in
                    // Обновляем смещение
                    height = gesture.translation.height + dragEnded
                }
                .onEnded { _ in
                    if height < 0 {
                        height = 0.0
                    }
                    dragEnded = height
                }
        )

    }
    .animation(.spring(), value: height)
    .frame(height: 35 + height + 20)
    .onTapGesture(perform: {
        print(rusOrNo)
    })
    .background(.clear)
    .foregroundColor(.clear)
}

}