-
Notifications
You must be signed in to change notification settings - Fork 32
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
Pasting trouble #175
Comments
Go into |
Okay, I've done some more fiddling. It seems to be okay with pasting large quantities of plain text (though I need to hammer it a little more to be sure). The problem is pasting HTML. If I grab some text from a random wikipedia page and try to paste it in, with a breakpoint in the MarkupWKWebView.pasteHTML handler, the breakpoint never triggers. It's like the app is completely ignoring the paste if it's a bunch of HTML. |
I would just back up further in the call chain, perhaps to |
Okay, a breakpoint in the first line of MarkupWKWebView.pasteableType will stop execution. It correctly identifies the stuff as HTML and returns that, which gets it back to the canPerformAction handler, but when that's done is goes back to pasteableType and identifies the html again, then it just seems to be caught in an infinite loop there. |
Yah, I don't know why that happens, but it's some kind of debugger breakpoint weirdness. If you remove the breakpoint, it won't be in an infinite loop. How about breaking at |
That helped. In the paste function, it's going into the HTML case and if I look at my data there, everything's fine – it's all intact. Going further in I find that it's surviving intact all the way through the escaping that you call in the evaluateJavaScript call in pasteHTML. I tried setting a breakpoint inside MU.pasteHTML so that I could look at the data there, but Xcode ignored it. Is that normal? |
To set a breakpoint in MU.pasteHTML, you have to use the Safari Web Inspector from the Developer Tools menu in Safari (https://developer.apple.com/documentation/safari-developer-tools/web-inspector). It's primitive, but an alternative is to insert |
@benlong100 I recently put in some fixes for pasting. If this is still broken on main, can you let me know, or otherwise, close the issue. Thanks. |
Sorry for the delay, I've been mired in another part of the code. I had not forked your code before – I was just running it as a Local Dependency. This morning I set things up properly for syncing to your changes, and got my alterations moved into the code. When I invoke the MarkupEditor it's not loading my text – the editor and all my toolbars show properly, but there's no document or caret. Has something changed in the way that I need to pass it source text? If not then I must have missed something when I moved my changes over. |
No, nothing should have changed. Since I'm often mucking around in |
My app is not showing up in the Device menu in Develop. This implies that I need to make the WKWebView inspectable. If that's true, where's the best place to put the .isInspectable = true? |
I do this where I initialize other things about the MarkupEditor (like toolbar stuff) by doing:
|
Okay, so the WebInspector is working now. If there were errors, were might I see them? Right now it's showing the html, which looks like this:
As expected, my content isn't in there. Hitting refresh in the Web Inspector doesn't change anything. In the section with the checkboxes for active, focus, focus-visible, and so on, nothing is checked. In the MarkupDelegate file, I put a breakpoint in the MarkupDidLoad handler, but it's never triggering. In the web inspector, when I mouse over the body tag, on the iPad it highlights a very thin strip across the top of the WKWebView. Sorry if these are silly questions, I've never used this WebInspector before. Seems a bit of a hack, but it's cool. |
You would not see any errors until you hit the reload button (3rd from the left in the top bar of the window). Reloading will cause the .js to load again, and if there was some change that doesn't compile properly, you would see an error show up in the console indicating the line number. The thin strip being highlighted is just showing the body. Maybe at this point a better approach is to debug more top-down. Maybe this kind of sequence:
Assuming you get this far, you can set a break on the From what you're describing, something is broken in this sequence, so hopefully this gives you a path to narrowing it down. |
Okay, that was good stuff. In MarkupWKWebView.initForEditing I found that html does contain my stuff. Where it comes apart is in MarkupCoordinator. loadedUserFiles isn't loading anything – both scriptFile and CSSFile are null. From what I see in your comments, that prevents loadInitialHTML? I have a customCSS file that is stored in a specific user directory. I pass that as the resourcesURL when I call MarkupEditorView. Should I be putting that somewhere else? And I have no idea what scriptFile might be referring to. |
Aha! I just realized you've added a lot of new documentation to the Code page here – I haven't looked at it in a long time. I'll dig through that and see if it clears this up. Sorry I didn't see that before. |
The This was definitely a change I made in the most recent round to allow custom scripts and css that I also added to the README. With this most recent change, you should be able to set your custom css file as discussed in the new README section. I am not sure how you are loading your custom CSS, but maybe the new work I did to formalize how to customize CSS is at cross purposes to what you did. In any case, if the
In your case, with both |
Okay, I've gone through your new readme. The new customization stuff is cool! And I've been wondering about changing the caret color but hadn't got around to it. As for a custom style sheet, when I set markupConfiguration.userCssFile can that be a URL to a file? Or is it always going to look in the Bundle? Because of some custom font stuff, I have to modify my CSS file at runtime, so I can't keep an accurate one in the bundle. |
It will always look for it in your app bundle. I'd be surprised if you can do much with files outside of the app bundle because of sandboxing rules. |
I have some custom fonts in my app bundle and I want to be able to reference them from the CSS file. To do that the CSS file has to know the path to the font file. Unfortunately, at runtime, the path to the bundle is never the same. For example, it might be something like this:
|
If you look at the SwiftUIDemo files like (now) custom.css, the file is part of the bundle, as is steve.png that I stuck in their for demo purposes so I could reference it from the demo.html. Can't you just put Future-Condensed.otf in the bundle like these other files, and then I would think your CSS can reference it without any path since it will be in the same place as the CSS. |
The fonts are in the bundle, so that will work for the Markup editor. However I have some other web views that run off of documents stored in the app's Documents folder, so I need a style sheet in there, as well. That's where I get into the trouble of the bundle address changing every time. Also, I have some preferences that allow the user to change the style sheet. Since the bundle is read-only, I can't work out of a bundle-based style sheet for that. |
I'm doing all the editing from the cache directory, copying things from the bundle in |
Okay, I've got my app changed around so that it does all of its work out of the ApplicationSupport directory. I have some things that I have to ensure will persist, so I'm hesitant to put them in the Caches folder. What I was doing with the old version of MarkupEditor was putting the CSS file and all of the images associated with the document I wanted to edit into a folder, and then passing a URL to that folder through the resourcesURL parameter in my MarkupEditorView call. I assume that, with the new scheme, that no longer works. I will still need to copy the images into the appropriate place, and from the Readme, it looks like that would be into the ID folder in the caches folder. I assume I could copy my custom css into there as well? What I'm not clear on is where I should add this copying code. I don't want to copy over a CSS file and then have something downstream write over it with the one from the bundle. |
I think specifying As a last resort, I was thinking about exposing the MarkupWKWebView |
Ah, copying the fonts into resourcesURL is very clever, that seems like the easiest solution. I wasn't clear on whether things were still being copied over from resourcesURL, so that's good news. I think that's a good mechanism and as long as it keeps working I can't think of a reason to expose the MarkupWKWebView baseUrl. So now I'm back to the problem of the MarkupEditor not loading properly. I can watch LoadUserFiles and see that both the scriptFile and cssFile are set to null and I can see it call evaluateJavaScript, but I gather it's not possible to set breakpoints in Javascript code in Xcode? 'Cause it's not letting me do that. In answer to your previous question of whether I have the most recent JS code, I just installed all this yesterday, so I should be in good shape. |
You can put in breaks in markup.js from the WebInspector, but since we are dealing with it in a state where it really hasn't fully loaded everything, it's hard to do that. Can you put in a break in |
Okay, it's doing that weird Xcode thing where it runs through the code twice. The first time, there's no error, but then it comes back through and the console shows: So then if I go look in the markup.js file I find a lovely MU.loadUserFile function that looks perfectly intact. |
Aha! I see markup.js as a source in the Web Inspector but when I search it for loadUserFiles there's nothing there. Eyeballed around manually for a bit and couldn't find it. How can it be showing up in Xcode but not here? |
The one in Xcode is not what was found in the bundle and copied to the
|
Okay, you just lost me. I’d already done some Clean Builds, but that didn’t help, but I don’t understand building the MarkupEditor Target. How do I rebuild just that? (And was I supposed to have done some kind of special build of it before?)
… On Jan 19, 2024, at 3:03 PM, Steve Harris ***@***.***> wrote:
The one in Xcode is not what was found in the bundle and copied to the cacheUrl() location in initRootFiles(). Maybe the first thing to do is Clean Build Folder, and make sure you build MarkupEditor build target again. If I had to guess, as you said, your app has a dependency on the local build of MarkupEditor, but you didn't rebuild it when you updated. So just rebuilding MarkupEditor might do the trick. If not, then you are down to debugging in initRootFiles and figuring out where it's finding markup.js and whether the contents are correct.
private func initRootFiles() {
guard
let rootHtml = url(forResource: "markup", withExtension: "html"),
let rootCss = url(forResource: "markup", withExtension: "css"),
let rootJs = url(forResource: "markup", withExtension: "js") else {
assertionFailure("Could not find markup.html, css, and js for this bundle.")
return
}
var srcUrls = [rootHtml, rootCss, rootJs]
// If specified, the userCSS comes from the app's main bundle, not something MarkupEditor provides
if let userCssFile, let userCss = url(forResource: userCssFile, withExtension: nil) {
srcUrls.append(userCss)
}
if let userScriptFile, let userScript = url(forResource: userScriptFile, withExtension: nil) {
srcUrls.append(userScript)
}
let fileManager = FileManager.default
// The cacheDir is a "id" subdirectory below the app's cache directory
// If not supplied, then id will be a UUID().uuidString
let cacheUrl = cacheUrl()
let cacheUrlPath = cacheUrl.path
do {
try fileManager.createDirectory(atPath: cacheUrlPath, withIntermediateDirectories: true, attributes: nil)
for srcUrl in srcUrls {
let dstUrl = cacheUrl.appendingPathComponent(srcUrl.lastPathComponent)
try? fileManager.removeItem(at: dstUrl)
try fileManager.copyItem(at: srcUrl, to: dstUrl)
}
} catch let error {
assertionFailure("Failed to set up cacheDir with root resource files: \(error.localizedDescription)")
}
}
—
Reply to this email directly, view it on GitHub <#175 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AD2PK523446RSGAD6BNKOPTYPL3V3AVCNFSM6AAAAABBA57ILCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBRGI3DQMRYHE>.
You are receiving this because you were mentioned.
|
If you cloned the repo, then open Xcode on the MarkupEditor project and build it. That creates the framework you are referencing in your app as a dependency, so the If you use the Swift Package dependency, it needs to be updated to the latest version (0.6.2 I think) |
Okay, that worked halfway! I rebuilt the MarkupEditor target and cleaned out the Package Cache in my app. As you may recall, I'm running two different editors and now one of them is working! The other is still throwing the same error and finding the same lack of MU.loadUserFiles, but I'll poke around and see if I can figure out what might be different. Thank you thank you! |
I have figured out what the problem was. As I mentioned before, before calling the editor I uses the resourcesURL to copy over a style sheet and any images my document needs. What I hadn't mentioned before is that I also copy over a markup.html file. I don't remember the details, but with the old version I seem to have discovered that, if I'm doing that, then I also have to copy markup.js through the resources directory, or MarkupEditor won't have one. To do that, I had made a copy of your markup.js file into my app bundle, and I was copying that into the resources directory when summoning a markup editor. What was happening earlier today was that, at runtime, it was still copying that OLD version of markup.js over. I deleted that old version, and commented out the sections that copied it into the resources directory and then and had a very strange experience. The editors started working, and so I started testing. After four or five entries everything stopped working – the editor would no longer load at all. I hadn't made any changes to the code – this all happened during a single execution of the app. After that it wouldn't work at all and the only way that I've been able to get it working again is to put a copy of the new markup.js folder back into my bundle and return to letting my code copy that over via the resources directory. Now everything is working fine. If I don't do that, then when I execute the code and view it in the Web Inspector in Safari I find that there's no JS source at all in the Markup Editor. So the behavior I seem to be seeing here is that, if you use resourcesURL to copy over a markup html and css file then you also have to copy over a js file. I'm fine with doing this and it's all working now, and perhaps I'm coming to the wrong conclusion, but that's what seems to be happening. And now, finally, after all these weeks I can address the original point of this whole thread: whatever you've changed through these recent updates has fixed my pasting problem! Thank you! |
Here's an odd one: occasionally, with large pastes, it's moving the first paragraph of the text I'm pasting to the very end. I'll see if I can find an example online that does this. Also, should Command-V be working as a shortcut? |
The issue with pasting misplacing the paragraph is going to have something to do with the structure of what's being pasted, not really the length. If you find a case that reproduces it, what would help me debug is the html passed to On command+V, yes it should work. You should see it in your Edit menu, and the item should be enabled. If it's not in the Edit menu or it's disabled, then it won't work. You need to have built the menu, tho. See AppDelegate.buildMenu(with:) in the SharedDemo content to see where that should be done. |
I'm having trouble getting the editor to paste a larger (roughly 1500 words) quantity of text. In both my app and the demo app, it gets through the first paragraph, which is exactly 50 words, and then nothing after that shows up.
The text was updated successfully, but these errors were encountered: