-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Desktop: Fixes #10733: Fix not-yet-created images lost while editing with the Rich Text Editor #10734
Merged
laurent22
merged 3 commits into
laurent22:dev
from
personalizedrefrigerator:pr/fix-nonexistent-resources-broken-by-rte
Jul 16, 2024
Merged
Desktop: Fixes #10733: Fix not-yet-created images lost while editing with the Rich Text Editor #10734
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/app-cli/tests/html_to_md/resource_placeholder.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<p>Markdown images:</p> | ||
<ul> | ||
<li> | ||
With ALT and title: | ||
<div | ||
class="not-loaded-resource not-loaded-image-resource resource-status-test" | ||
data-original-alt="test" | ||
data-original-title="testing" | ||
data-resource-id="0415d61cc33e47afa6dde45948c3177f" | ||
> | ||
<img src="data:image/svg+xml;utf8,some-icon-here"/> | ||
</div> | ||
</li> | ||
<li> | ||
With neither ALT nor title: | ||
<div | ||
class="not-loaded-resource not-loaded-image-resource resource-status-error" | ||
data-original-alt="" | ||
data-original-title="" | ||
data-resource-id="0a25d61cc33e57afa6dde45948c3177f" | ||
> | ||
<img src="data:image/svg+xml;utf8,some-icon-here"/> | ||
</div> | ||
</li> | ||
</ul> | ||
|
||
<p>HTML images:</p> | ||
<ul> | ||
<li> | ||
<div | ||
class="not-loaded-resource not-loaded-image-resource resource-status-error" | ||
data-original-before=" width="230"" | ||
data-original-after=" style="border: 32px inset red;"/" | ||
data-resource-id="0415d61cc33e47afa6dde45948c3177f" | ||
> | ||
<img src="data:image/svg+xml;utf8,some-icon-here"/> | ||
</div> | ||
</li> | ||
<li> | ||
<div | ||
class="not-loaded-resource not-loaded-image-resource resource-status-error" | ||
data-original-after="/" | ||
data-resource-id="0415d61cc33e47afa6dde45948c3177f" | ||
> | ||
<img src="data:image/svg+xml;utf8,some-icon-here"/> | ||
</div> | ||
</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Markdown images: | ||
|
||
- With ALT and title:![test](:/0415d61cc33e47afa6dde45948c3177f "testing") | ||
- With neither ALT nor title:![](:/0a25d61cc33e57afa6dde45948c3177f) | ||
|
||
HTML images: | ||
|
||
- <img width="230" src=":/0415d61cc33e47afa6dde45948c3177f" style="border: 32px inset red;"/> | ||
- <img src=":/0415d61cc33e47afa6dde45948c3177f" /> |
15 changes: 15 additions & 0 deletions
15
packages/app-cli/tests/md_to_html/resource_nonexistent_image.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="not-loaded-resource not-loaded-image-resource resource-status-notDownloaded" data-resource-id="a1test2a1test2a1test2a1test22345" data-original-alt data-original-title="test" contenteditable="false"><img src="data:image/svg+xml;utf8, | ||
		<svg width="1700" height="1536" xmlns="http://www.w3.org/2000/svg"> | ||
		 <path d="M1280 1344c0-35-29-64-64-64s-64 29-64 64 29 64 64 64 64-29 64-64zm256 0c0-35-29-64-64-64s-64 29-64 64 29 64 64 64 64-29 64-64zm128-224v320c0 53-43 96-96 96H96c-53 0-96-43-96-96v-320c0-53 43-96 96-96h465l135 136c37 36 85 56 136 56s99-20 136-56l136-136h464c53 0 96 43 96 96zm-325-569c10 24 5 52-14 70l-448 448c-12 13-29 19-45 19s-33-6-45-19L339 621c-19-18-24-46-14-70 10-23 33-39 59-39h256V64c0-35 29-64 64-64h256c35 0 64 29 64 64v448h256c26 0 49 16 59 39z"/> | ||
		</svg> | ||
	"/></div> | ||
<div class="not-loaded-resource not-loaded-image-resource resource-status-notDownloaded" data-resource-id="a1test2a1test2a1test2a1test22346" data-original-alt="test" data-original-title contenteditable="false"><img src="data:image/svg+xml;utf8, | ||
		<svg width="1700" height="1536" xmlns="http://www.w3.org/2000/svg"> | ||
		 <path d="M1280 1344c0-35-29-64-64-64s-64 29-64 64 29 64 64 64 64-29 64-64zm256 0c0-35-29-64-64-64s-64 29-64 64 29 64 64 64 64-29 64-64zm128-224v320c0 53-43 96-96 96H96c-53 0-96-43-96-96v-320c0-53 43-96 96-96h465l135 136c37 36 85 56 136 56s99-20 136-56l136-136h464c53 0 96 43 96 96zm-325-569c10 24 5 52-14 70l-448 448c-12 13-29 19-45 19s-33-6-45-19L339 621c-19-18-24-46-14-70 10-23 33-39 59-39h256V64c0-35 29-64 64-64h256c35 0 64 29 64 64v448h256c26 0 49 16 59 39z"/> | ||
		</svg> | ||
	"/></div> | ||
<div class="not-loaded-resource not-loaded-image-resource resource-status-notDownloaded" data-resource-id="a1test2a1test2a1test2a1test22347" data-original-before=" " data-original-after=" class="jop-noMdConv"/" contenteditable="false"><img src="data:image/svg+xml;utf8, | ||
		<svg width="1700" height="1536" xmlns="http://www.w3.org/2000/svg"> | ||
		 <path d="M1280 1344c0-35-29-64-64-64s-64 29-64 64 29 64 64 64 64-29 64-64zm256 0c0-35-29-64-64-64s-64 29-64 64 29 64 64 64 64-29 64-64zm128-224v320c0 53-43 96-96 96H96c-53 0-96-43-96-96v-320c0-53 43-96 96-96h465l135 136c37 36 85 56 136 56s99-20 136-56l136-136h464c53 0 96 43 96 96zm-325-569c10 24 5 52-14 70l-448 448c-12 13-29 19-45 19s-33-6-45-19L339 621c-19-18-24-46-14-70 10-23 33-39 59-39h256V64c0-35 29-64 64-64h256c35 0 64 29 64 64v448h256c26 0 49 16 59 39z"/> | ||
		</svg> | ||
	"/></div> |
3 changes: 3 additions & 0 deletions
3
packages/app-cli/tests/md_to_html/resource_nonexistent_image.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
![](:/a1test2a1test2a1test2a1test22345 "test") | ||
![test](:/a1test2a1test2a1test2a1test22346) | ||
<img src=":/a1test2a1test2a1test2a1test22347"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,43 @@ rules.foregroundColor = { | |
}, | ||
} | ||
|
||
// Converts placeholders for not-loaded resources. | ||
rules.resourcePlaceholder = { | ||
filter: function (node, options) { | ||
if (!options.allowResourcePlaceholders) return false; | ||
if (!node.classList || !node.classList.contains('not-loaded-resource')) return false; | ||
const isImage = node.classList.contains('not-loaded-image-resource'); | ||
if (!isImage) return false; | ||
|
||
const resourceId = node.getAttribute('data-resource-id'); | ||
return resourceId && resourceId.match(/^[a-z0-9]{32}$/); | ||
}, | ||
|
||
replacement: function (_content, node) { | ||
const htmlBefore = node.getAttribute('data-original-before') || ''; | ||
const htmlAfter = node.getAttribute('data-original-after') || ''; | ||
const isHtml = htmlBefore || htmlAfter; | ||
const resourceId = node.getAttribute('data-resource-id'); | ||
if (isHtml) { | ||
const attrs = [ | ||
htmlBefore.trim(), | ||
`src=":/${resourceId}"`, | ||
htmlAfter.trim(), | ||
].filter(a => !!a); | ||
|
||
return `<img ${attrs.join(' ')}>`; | ||
Comment on lines
+156
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: Try to reduce future code duplication: Pass a function through At present, much of this logic will need to be duplicated when fixing #10733 for HTML notes. |
||
} else { | ||
const originalAltText = node.getAttribute('data-original-alt') || ''; | ||
const title = node.getAttribute('data-original-title'); | ||
return imageMarkdownFromAttributes({ | ||
alt: originalAltText, | ||
title, | ||
src: `:/${resourceId}`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// ============================== | ||
// END Joplin format support | ||
// ============================== | ||
|
@@ -510,6 +547,14 @@ rules.code = { | |
} | ||
} | ||
|
||
function imageMarkdownFromAttributes(attributes) { | ||
var alt = attributes.alt || '' | ||
var src = filterLinkHref(attributes.src || '') | ||
var title = attributes.title || '' | ||
var titlePart = title ? ' "' + filterImageTitle(title) + '"' : '' | ||
return src ? '![' + alt.replace(/([[\]])/g, '\\$1') + ']' + '(' + src + titlePart + ')' : '' | ||
} | ||
|
||
function imageMarkdownFromNode(node, options = null) { | ||
options = Object.assign({}, { | ||
preserveImageTagsWithSize: false, | ||
|
@@ -519,11 +564,11 @@ function imageMarkdownFromNode(node, options = null) { | |
return node.outerHTML; | ||
} | ||
|
||
var alt = node.alt || '' | ||
var src = filterLinkHref(node.getAttribute('src') || '') | ||
var title = node.title || '' | ||
var titlePart = title ? ' "' + filterImageTitle(title) + '"' : '' | ||
return src ? '![' + alt.replace(/([[\]])/g, '\\$1') + ']' + '(' + src + titlePart + ')' : '' | ||
return imageMarkdownFromAttributes({ | ||
alt: node.alt, | ||
src: node.getAttribute('src'), | ||
title: node.title, | ||
}); | ||
} | ||
|
||
function imageUrlFromSource(node) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class="jop-noMdConv"
added todata-original-after
?