Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
f5010577
Unverified
Commit
f5010577
authored
1 year ago
by
fusagiko / takayamaki
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Add type annotation for Blurhash component (#24750)
parent
5a5975d7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/javascript/mastodon/components/blurhash.tsx
+45
-0
45 additions, 0 deletions
app/javascript/mastodon/components/blurhash.tsx
with
45 additions
and
0 deletions
app/javascript/mastodon/components/blurhash.
j
sx
→
app/javascript/mastodon/components/blurhash.
t
sx
+
45
−
0
View file @
f5010577
// @ts-check
import
{
decode
}
from
'
blurhash
'
;
import
React
,
{
useRef
,
useEffect
}
from
'
react
'
;
import
PropTypes
from
'
prop-types
'
;
/**
* @typedef BlurhashPropsBase
* @property {string?} hash Hash to render
* @property {number} width
* Width of the blurred region in pixels. Defaults to 32
* @property {number} [height]
* Height of the blurred region in pixels. Defaults to width
* @property {boolean} [dummy]
* Whether dummy mode is enabled. If enabled, nothing is rendered
* and canvas left untouched
*/
/** @typedef {JSX.IntrinsicElements['canvas'] & BlurhashPropsBase} BlurhashProps */
/**
* Component that is used to render blurred of blurhash string
* @param {BlurhashProps} param1 Props of the component
* @returns {JSX.Element} Canvas which will render blurred region element to embed
*/
type
Props
=
{
hash
:
string
;
width
?:
number
;
height
?:
number
;
dummy
?:
boolean
;
// Whether dummy mode is enabled. If enabled, nothing is rendered and canvas left untouched
children
?:
never
;
[
key
:
string
]:
any
;
}
function
Blurhash
({
hash
,
width
=
32
,
height
=
width
,
dummy
=
false
,
...
canvasProps
})
{
const
canvasRef
=
/** @type {import('react').MutableRefObject
<HTMLCanvasElement>
} */
(
useRef
()
);
}
:
Props
)
{
const
canvasRef
=
useRef
<
HTMLCanvasElement
>
(
null
);
useEffect
(()
=>
{
const
{
current
:
canvas
}
=
canvasRef
;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const
canvas
=
canvasRef
.
current
!
;
// eslint-disable-next-line no-self-assign
canvas
.
width
=
canvas
.
width
;
// resets canvas
if
(
dummy
||
!
hash
)
return
;
...
...
@@ -43,8 +31,7 @@ function Blurhash({
const
ctx
=
canvas
.
getContext
(
'
2d
'
);
const
imageData
=
new
ImageData
(
pixels
,
width
,
height
);
// @ts-expect-error
ctx
.
putImageData
(
imageData
,
0
,
0
);
ctx
?.
putImageData
(
imageData
,
0
,
0
);
}
catch
(
err
)
{
console
.
error
(
'
Blurhash decoding failure
'
,
{
err
,
hash
});
}
...
...
@@ -55,11 +42,4 @@ function Blurhash({
);
}
Blurhash
.
propTypes
=
{
hash
:
PropTypes
.
string
.
isRequired
,
width
:
PropTypes
.
number
,
height
:
PropTypes
.
number
,
dummy
:
PropTypes
.
bool
,
};
export
default
React
.
memo
(
Blurhash
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment