Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When loading json into the editor control the double quotes get replaces with invalid quotes #18220

Closed
JoacimWall opened this issue Oct 21, 2023 · 6 comments
Labels
area-controls-editor Editor blocked Work that is currently blocked platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst s/needs-attention Issue has more information and needs another look t/bug Something isn't working

Comments

@JoacimWall
Copy link

JoacimWall commented Oct 21, 2023

Description

Hi
the Editor control in MAUI is replacing the double quotes with invalid quotes the make the file invalid to parse as Json.
The char below
image
is replaced with this before the word
image
and this after the word with quotes
image

that create a invalid json

Steps to Reproduce

//This is binded to the MAUI Editor Control
private string editFileText;
    public string EditFileText
    {
        get { return editFileText; }
        set { SetProperty(ref editFileText, value); }
    }

//Read file in to property
private async Task ReadFile()
{
// open a valid Jason 
  using Stream fileStream = System.IO.File.OpenRead(filepath);
    using StreamReader reader = new StreamReader(fileStream,System.Text.Encoding.UTF8);
   
    var c = await reader.ReadToEndAsync();
  
   EditFileText = c;
 }
private async Task Save()
    {
        using var dlg = DialogService.GetProgress("");
        using FileStream outputStream = System.IO.File.OpenWrite(currentOpenFilePath);
        using StreamWriter streamWriter = new StreamWriter(outputStream,System.Text.Encoding.UTF8);
        await streamWriter.WriteAsync(editFileText);
       // await streamWriter.WriteAsync(text);
    }

Link to public reproduction project repository

https://github.com/JoacimWall/Visual-studio-multilingual-extension/tree/main/src/MultilingualClient

Version with bug

8.0.0-rc.2.9373

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS, macOS

Affected platform versions

No response

Did you find any workaround?

If I skip binding the EditFileText to the editor control and just load the json into the string and then save the string the quotes is not replaced.

Relevant log output

No response

@JoacimWall JoacimWall added the t/bug Something isn't working label Oct 21, 2023
@PureWeen PureWeen added the s/needs-repro Attach a solution or code which reproduces the issue label Oct 24, 2023
@ghost
Copy link

ghost commented Oct 24, 2023

Hi @JoacimWall. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@JoacimWall
Copy link
Author

Hi @JoacimWall. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

Done

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-repro Attach a solution or code which reproduces the issue labels Oct 25, 2023
@Eilon Eilon added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Oct 25, 2023
@mattleibow
Copy link
Member

mattleibow commented Oct 25, 2023

This is pretty insane, but the OS is deciding that you want these nice quotes. https://stackoverflow.com/questions/44932863/ios-11-disable-smart-quotes

I tried setting this and it helps with keyboard-entered text, but this is more insane:

I would also add that it only prevents the smart quote from being entered from the keyboard. However, the user can type a smart quote from another app and copy/paste it into your smartQuotesType = .no text field.

The OS can just bypass your attempts!

This is the "fix":

The best way to 'truly' prevent the smart quotes is to implement the delegate textField:shouldChangeCharactersInRange:replacementString: and detect a smart quote and replace it with a normal apostrophe.

Not sure if I accept this yet.

@mattleibow
Copy link
Member

mattleibow commented Oct 25, 2023

I can reproduce this in Xcode whith this code:

import UIKit

class ViewController: UIViewController {

    var extractedExpr: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        extractedExpr = UITextView(frame: CGRect(x: 0, y: 0, width: 800, height: 200))
        extractedExpr.smartQuotesType = UITextSmartQuotesType.no
        
        let btn = UIButton(type: UIButton.ButtonType.system)
        btn.frame = CGRect(x: 0, y: 210, width: 800, height: 40)
        btn.setTitle("Go!", for: UIControl.State.normal)
        btn.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
        
        self.view.addSubview(extractedExpr);
        self.view.addSubview(btn);
        
    }
    
    @objc func buttonAction(_ sender:UIButton!) {
       print("Button tapped")
        
        extractedExpr.replace(
            extractedExpr.textRange(
                from: extractedExpr.beginningOfDocument,
                to: extractedExpr.endOfDocument)!,
            withText: "")
        
        extractedExpr.replace(
            extractedExpr.textRange(
                from: extractedExpr.beginningOfDocument,
                to: extractedExpr.endOfDocument)!,
            withText: "Hello \"Matthew\"!")
    }
}
  1. Launch the app
  2. Press the button and notice the fancy quotes
  3. Focus the editor
  4. Press the button and notice the boring quotes

Seems this smartQuotesType property only kicks in after the first focus...

@mattleibow
Copy link
Member

mattleibow commented Oct 26, 2023

OK, so I have a workaround. I am sure this smart quotes not working until you focus the editor is an OS bug since it happens in Xcode. Or maybe it is by design.

But, with that knowledge, I have a workaround. To fix the cases where typing in the editor autoformats the text, you can add this to your MauiProgram.cs:

.ConfigureMauiHandlers(_ =>
{
#if MACCATALYST
    Microsoft.Maui.Handlers.EditorHandler.Mapper.AppendToMapping("Smart", (h, v) =>
    {
        h.PlatformView.SmartQuotesType = UIKit.UITextSmartQuotesType.No;
        h.PlatformView.SmartDashesType = UIKit.UITextSmartDashesType.No;
        h.PlatformView.SmartInsertDeleteType = UIKit.UITextSmartInsertDeleteType.No;
    });
#endif
})

And then to fix the case of needing a focus, I did this in the code behind of MainPage.xaml.cs:

public MainPage(MainViewModel vm)
{
    // ...
    Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, EventArgs e)
{
    fileEditor.Focus();
    fileEditor.Unfocus();
}

Let me know if this helps.

@mattleibow mattleibow added blocked Work that is currently blocked s/needs-info Issue needs more info from the author and removed s/needs-attention Issue has more information and needs another look labels Oct 26, 2023
@dotnet dotnet deleted a comment Oct 26, 2023
@JoacimWall
Copy link
Author

Hi
Thanks the workaround solves the problem.

//Joacim

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels Oct 27, 2023
@PureWeen PureWeen closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2023
@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-editor Editor blocked Work that is currently blocked platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants