Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
167e5596
Unverified
Commit
167e5596
authored
Jul 16, 2022
by
boojack
Committed by
GitHub
Jul 16, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: generate html image in safari (#123)
parent
2a1e34fe
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
43 deletions
+56
-43
getCloneStyledElement.ts
web/src/labs/html2image/getCloneStyledElement.ts
+23
-24
index.ts
web/src/labs/html2image/index.ts
+11
-14
waitImageLoaded.ts
web/src/labs/html2image/waitImageLoaded.ts
+17
-0
share-memo-image-dialog.less
web/src/less/share-memo-image-dialog.less
+5
-5
No files found.
web/src/labs/html2image/getCloneStyledElement.ts
View file @
167e5596
import
convertResourceToDataURL
from
"./convertResourceToDataURL"
;
import
convertResourceToDataURL
from
"./convertResourceToDataURL"
;
const
getCloneStyledElement
=
async
(
element
:
HTMLElement
)
=>
{
const
applyStyles
=
async
(
sourceElement
:
HTMLElement
,
clonedElement
:
HTMLElement
)
=>
{
const
clonedElementContainer
=
document
.
createElement
(
element
.
tagName
);
clonedElementContainer
.
innerHTML
=
element
.
innerHTML
;
const
applyStyles
=
async
(
sourceElement
:
HTMLElement
,
clonedElement
:
HTMLElement
)
=>
{
if
(
!
sourceElement
||
!
clonedElement
)
{
if
(
!
sourceElement
||
!
clonedElement
)
{
return
;
return
;
}
}
const
sourceStyles
=
window
.
getComputedStyle
(
sourceElement
);
if
(
sourceElement
.
tagName
===
"IMG"
)
{
if
(
sourceElement
.
tagName
===
"IMG"
)
{
try
{
try
{
const
url
=
await
convertResourceToDataURL
(
sourceElement
.
getAttribute
(
"src"
)
??
""
);
const
url
=
await
convertResourceToDataURL
(
sourceElement
.
getAttribute
(
"src"
)
??
""
);
...
@@ -20,6 +14,7 @@ const getCloneStyledElement = async (element: HTMLElement) => {
...
@@ -20,6 +14,7 @@ const getCloneStyledElement = async (element: HTMLElement) => {
}
}
}
}
const
sourceStyles
=
window
.
getComputedStyle
(
sourceElement
);
for
(
const
item
of
sourceStyles
)
{
for
(
const
item
of
sourceStyles
)
{
clonedElement
.
style
.
setProperty
(
item
,
sourceStyles
.
getPropertyValue
(
item
),
sourceStyles
.
getPropertyPriority
(
item
));
clonedElement
.
style
.
setProperty
(
item
,
sourceStyles
.
getPropertyValue
(
item
),
sourceStyles
.
getPropertyPriority
(
item
));
}
}
...
@@ -27,7 +22,11 @@ const getCloneStyledElement = async (element: HTMLElement) => {
...
@@ -27,7 +22,11 @@ const getCloneStyledElement = async (element: HTMLElement) => {
for
(
let
i
=
0
;
i
<
clonedElement
.
childElementCount
;
i
++
)
{
for
(
let
i
=
0
;
i
<
clonedElement
.
childElementCount
;
i
++
)
{
await
applyStyles
(
sourceElement
.
children
[
i
]
as
HTMLElement
,
clonedElement
.
children
[
i
]
as
HTMLElement
);
await
applyStyles
(
sourceElement
.
children
[
i
]
as
HTMLElement
,
clonedElement
.
children
[
i
]
as
HTMLElement
);
}
}
};
};
const
getCloneStyledElement
=
async
(
element
:
HTMLElement
)
=>
{
const
clonedElementContainer
=
document
.
createElement
(
element
.
tagName
);
clonedElementContainer
.
innerHTML
=
element
.
innerHTML
;
await
applyStyles
(
element
,
clonedElementContainer
);
await
applyStyles
(
element
,
clonedElementContainer
);
...
...
web/src/labs/html2image/index.ts
View file @
167e5596
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
* 2. <foreignObject>: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject
* 2. <foreignObject>: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject
*/
*/
import
getCloneStyledElement
from
"./getCloneStyledElement"
;
import
getCloneStyledElement
from
"./getCloneStyledElement"
;
import
waitImageLoaded
from
"./waitImageLoaded"
;
type
Options
=
Partial
<
{
type
Options
=
Partial
<
{
backgroundColor
:
string
;
backgroundColor
:
string
;
...
@@ -51,7 +52,6 @@ const generateSVGElement = (width: number, height: number, element: HTMLElement)
...
@@ -51,7 +52,6 @@ const generateSVGElement = (width: number, height: number, element: HTMLElement)
export
const
toSVG
=
async
(
element
:
HTMLElement
,
options
?:
Options
)
=>
{
export
const
toSVG
=
async
(
element
:
HTMLElement
,
options
?:
Options
)
=>
{
const
{
width
,
height
}
=
getElementSize
(
element
);
const
{
width
,
height
}
=
getElementSize
(
element
);
const
clonedElement
=
await
getCloneStyledElement
(
element
);
const
clonedElement
=
await
getCloneStyledElement
(
element
);
if
(
options
?.
backgroundColor
)
{
if
(
options
?.
backgroundColor
)
{
...
@@ -65,11 +65,6 @@ export const toSVG = async (element: HTMLElement, options?: Options) => {
...
@@ -65,11 +65,6 @@ export const toSVG = async (element: HTMLElement, options?: Options) => {
};
};
export
const
toCanvas
=
async
(
element
:
HTMLElement
,
options
?:
Options
):
Promise
<
HTMLCanvasElement
>
=>
{
export
const
toCanvas
=
async
(
element
:
HTMLElement
,
options
?:
Options
):
Promise
<
HTMLCanvasElement
>
=>
{
const
url
=
await
toSVG
(
element
,
options
);
const
imageEl
=
new
Image
();
imageEl
.
src
=
url
;
const
ratio
=
options
?.
pixelRatio
||
1
;
const
ratio
=
options
?.
pixelRatio
||
1
;
const
{
width
,
height
}
=
getElementSize
(
element
);
const
{
width
,
height
}
=
getElementSize
(
element
);
...
@@ -91,18 +86,20 @@ export const toCanvas = async (element: HTMLElement, options?: Options): Promise
...
@@ -91,18 +86,20 @@ export const toCanvas = async (element: HTMLElement, options?: Options): Promise
context
.
fillRect
(
0
,
0
,
canvas
.
width
,
canvas
.
height
);
context
.
fillRect
(
0
,
0
,
canvas
.
width
,
canvas
.
height
);
}
}
return
new
Promise
((
resolve
)
=>
{
const
url
=
await
toSVG
(
element
,
options
);
imageEl
.
onload
=
()
=>
{
const
imageEl
=
new
Image
();
imageEl
.
style
.
zIndex
=
"-1"
;
imageEl
.
style
.
position
=
"absolute"
;
imageEl
.
style
.
top
=
"0"
;
document
.
body
.
append
(
imageEl
);
await
waitImageLoaded
(
imageEl
,
url
);
context
.
drawImage
(
imageEl
,
0
,
0
,
canvas
.
width
,
canvas
.
height
);
context
.
drawImage
(
imageEl
,
0
,
0
,
canvas
.
width
,
canvas
.
height
);
imageEl
.
remove
();
resolve
(
canvas
);
return
canvas
;
};
});
};
};
const
toImage
=
async
(
element
:
HTMLElement
,
options
?:
Options
)
=>
{
const
toImage
=
async
(
element
:
HTMLElement
,
options
?:
Options
)
=>
{
const
canvas
=
await
toCanvas
(
element
,
options
);
const
canvas
=
await
toCanvas
(
element
,
options
);
return
canvas
.
toDataURL
();
return
canvas
.
toDataURL
();
};
};
...
...
web/src/labs/html2image/waitImageLoaded.ts
0 → 100644
View file @
167e5596
const
waitImageLoaded
=
(
image
:
HTMLImageElement
,
url
:
string
):
Promise
<
void
>
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
image
.
loading
=
"eager"
;
image
.
onload
=
()
=>
{
// NOTE: There is image loading problem in Safari, fix it with some trick
setTimeout
(()
=>
{
resolve
();
},
200
);
};
image
.
onerror
=
()
=>
{
reject
(
"Image load failed"
);
};
image
.
src
=
url
;
});
};
export
default
waitImageLoaded
;
web/src/less/share-memo-image-dialog.less
View file @
167e5596
...
@@ -63,14 +63,14 @@
...
@@ -63,14 +63,14 @@
@apply flex flex-row justify-start items-center w-full py-3 px-6;
@apply flex flex-row justify-start items-center w-full py-3 px-6;
> .normal-text {
> .normal-text {
@apply w-full flex flex-row justify-start items-center text-sm text-gray-500;
@apply w-full flex flex-row justify-start items-center text-sm
leading-6
text-gray-500;
> .
name
-text {
> .
icon
-text {
@apply text-
black ml-2
;
@apply text-
lg ml-1 mr-2 leading-6
;
}
}
> .
icon
-text {
> .
name
-text {
@apply text-
lg ml-1 mr-2
;
@apply text-
black ml-2 leading-6
;
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment