Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
Hubs
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
Container Registry
Model registry
Operate
Environments
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ar-noc
Hubs
Commits
efe80105
Commit
efe80105
authored
6 years ago
by
Greg Fodor
Browse files
Options
Downloads
Patches
Plain Diff
Refactor code to break out spawning routine
parent
085bd79e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/react-components/chat-message.js
+111
-0
111 additions, 0 deletions
src/react-components/chat-message.js
src/react-components/presence-log.js
+1
-108
1 addition, 108 deletions
src/react-components/presence-log.js
with
112 additions
and
108 deletions
src/react-components/chat-message.js
0 → 100644
+
111
−
0
View file @
efe80105
import
React
from
"
react
"
;
import
ReactDOM
from
"
react-dom
"
;
import
PropTypes
from
"
prop-types
"
;
import
styles
from
"
../assets/stylesheets/presence-log.scss
"
;
import
classNames
from
"
classnames
"
;
import
Linkify
from
"
react-linkify
"
;
import
{
toArray
as
toEmojis
}
from
"
react-emoji-render
"
;
import
serializeElement
from
"
../utils/serialize-element
"
;
const
messageCanvas
=
document
.
createElement
(
"
canvas
"
);
const
emojiRegex
=
/
(?:[\u
2700-
\u
27bf
]
|
(?:\u
d83c
[\u
dde6-
\u
ddff
]){2}
|
[\u
d800-
\u
dbff
][\u
dc00-
\u
dfff
]
|
[\u
0023-
\u
0039
]\u
fe0f
?\u
20e3|
\u
3299|
\u
3297|
\u
303d|
\u
3030|
\u
24c2|
\u
d83c
[\u
dd70-
\u
dd71
]
|
\u
d83c
[\u
dd7e-
\u
dd7f
]
|
\u
d83c
\u
dd8e|
\u
d83c
[\u
dd91-
\u
dd9a
]
|
\u
d83c
[\u
dde6-
\u
ddff
]
|
[\u
d83c[
\u
de01-
\u
de02
]
|
\u
d83c
\u
de1a|
\u
d83c
\u
de2f|
[\u
d83c[
\u
de32-
\u
de3a
]
|
[\u
d83c[
\u
de50-
\u
de51
]
|
\u
203c|
\u
2049|
[\u
25aa-
\u
25ab
]
|
\u
25b6|
\u
25c0|
[\u
25fb-
\u
25fe
]
|
\u
00a9|
\u
00ae|
\u
2122|
\u
2139|
\u
d83c
\u
dc04|
[\u
2600-
\u
26FF
]
|
\u
2b05|
\u
2b06|
\u
2b07|
\u
2b1b|
\u
2b1c|
\u
2b50|
\u
2b55|
\u
231a|
\u
231b|
\u
2328|
\u
23cf|
[\u
23e9-
\u
23f3
]
|
[\u
23f8-
\u
23fa
]
|
\u
d83c
\u
dccf|
\u
2934|
\u
2935|
[\u
2190-
\u
21ff
])
/
;
const
urlRegex
=
/^https
?
:
\/\/(
www
\.)?[
-a-zA-Z0-9@:%._+~#=
]{2,256}\.[
a-z
]{2,6}\b([
-a-zA-Z0-9@:%_+.~#?&
//
=
]
*
)
$/
;
const
messageBodyDom
=
body
=>
{
// Support wrapping text in ` to get monospace, and multiline.
const
multiLine
=
body
.
split
(
"
\n
"
).
length
>
1
;
const
mono
=
body
.
startsWith
(
"
`
"
)
&&
body
.
endsWith
(
"
`
"
);
const
messageBodyClasses
=
{
[
styles
.
messageBody
]:
true
,
[
styles
.
messageBodyMulti
]:
multiLine
,
[
styles
.
messageBodyMono
]:
mono
};
const
cleanedBody
=
(
mono
?
body
.
substring
(
1
,
body
.
length
-
1
)
:
body
).
trim
();
return
(
<
div
className
=
{
classNames
(
messageBodyClasses
)}
>
<
Linkify
properties
=
{{
target
:
"
_blank
"
,
rel
:
"
noopener referrer
"
}}
>
{
toEmojis
(
cleanedBody
)}
<
/Linkify
>
<
/div
>
);
};
export
function
spawnChatMessage
(
body
)
{
if
(
body
.
match
(
urlRegex
))
{
document
.
querySelector
(
"
a-scene
"
).
emit
(
"
add_media
"
,
body
);
return
;
}
const
isOneLine
=
body
.
split
(
"
\n
"
).
length
===
1
;
const
context
=
messageCanvas
.
getContext
(
"
2d
"
);
const
emoji
=
toEmojis
(
body
);
const
isEmoji
=
emoji
.
length
===
1
&&
emoji
[
0
].
props
&&
emoji
[
0
].
props
.
children
.
match
&&
emoji
[
0
].
props
.
children
.
match
(
emojiRegex
);
const
el
=
document
.
createElement
(
"
div
"
);
el
.
setAttribute
(
"
xmlns
"
,
"
http://www.w3.org/1999/xhtml
"
);
el
.
setAttribute
(
"
class
"
,
`
${
styles
.
presenceLog
}
${
styles
.
presenceLogSpawn
}
`
);
// The element is added to the DOM in order to have layout compute the width & height,
// and then it is removed after being rendered.
document
.
body
.
appendChild
(
el
);
const
entryDom
=
(
<
div
className
=
{
classNames
({
[
styles
.
presenceLogEntry
]:
!
isEmoji
,
[
styles
.
presenceLogEntryOneLine
]:
!
isEmoji
&&
isOneLine
,
[
styles
.
presenceLogEmoji
]:
isEmoji
})}
>
{
messageBodyDom
(
body
)}
<
/div
>
);
ReactDOM
.
render
(
entryDom
,
el
,
()
=>
{
// Scale by 12x
messageCanvas
.
width
=
el
.
offsetWidth
*
12
;
messageCanvas
.
height
=
el
.
offsetHeight
*
12
;
const
xhtml
=
encodeURIComponent
(
`
<svg xmlns="http://www.w3.org/2000/svg" width="
${
messageCanvas
.
width
}
" height="
${
messageCanvas
.
height
}
">
<foreignObject width="8.333%" height="8.333%" style="transform: scale(12.0);">
${
serializeElement
(
el
)}
</foreignObject>
</svg>
`
);
const
img
=
new
Image
();
img
.
onload
=
async
()
=>
{
context
.
drawImage
(
img
,
0
,
0
);
const
blob
=
await
new
Promise
(
resolve
=>
messageCanvas
.
toBlob
(
resolve
));
document
.
querySelector
(
"
a-scene
"
).
emit
(
"
add_media
"
,
new
File
([
blob
],
"
message.png
"
,
{
type
:
"
image/png
"
}));
el
.
parentNode
.
removeChild
(
el
);
};
img
.
src
=
"
data:image/svg+xml,
"
+
xhtml
;
});
}
export
default
function
ChatMessage
(
props
)
{
const
isOneLine
=
props
.
body
.
split
(
"
\n
"
).
length
===
1
;
return
(
<
div
className
=
{
props
.
className
}
>
{
props
.
maySpawn
&&
<
button
className
=
{
styles
.
spawnMessage
}
onClick
=
{()
=>
spawnChatMessage
(
props
.
body
)}
/>
}
<
div
className
=
{
isOneLine
?
styles
.
messageWrap
:
styles
.
messageWrapMulti
}
>
<
div
className
=
{
styles
.
messageSource
}
>
<
b
>
{
props
.
name
}
<
/b>
:
<
/div
>
{
messageBodyDom
(
props
.
body
)}
<
/div
>
<
/div
>
);
}
ChatMessage
.
propTypes
=
{
name
:
PropTypes
.
string
,
maySpawn
:
PropTypes
.
bool
,
body
:
PropTypes
.
string
,
className
:
PropTypes
.
string
};
This diff is collapsed.
Click to expand it.
src/react-components/presence-log.js
+
1
−
108
View file @
efe80105
import
React
,
{
Component
}
from
"
react
"
;
import
ReactDOM
from
"
react-dom
"
;
import
PropTypes
from
"
prop-types
"
;
import
styles
from
"
../assets/stylesheets/presence-log.scss
"
;
import
classNames
from
"
classnames
"
;
import
Linkify
from
"
react-linkify
"
;
import
{
toArray
as
toEmojis
}
from
"
react-emoji-render
"
;
import
{
FormattedMessage
}
from
"
react-intl
"
;
import
serializeElement
from
"
../utils/serialize-element
"
;
const
messageCanvas
=
document
.
createElement
(
"
canvas
"
);
const
presenceLogSpawnedStyle
=
`background-color: white; color: black; padding: 8px 16px; border-radius: 16px;`
;
const
presenceLogPureEmojiStyle
=
`background-color: transparent; text-align: center;`
;
const
emojiRegex
=
/
(?:[\u
2700-
\u
27bf
]
|
(?:\u
d83c
[\u
dde6-
\u
ddff
]){2}
|
[\u
d800-
\u
dbff
][\u
dc00-
\u
dfff
]
|
[\u
0023-
\u
0039
]\u
fe0f
?\u
20e3|
\u
3299|
\u
3297|
\u
303d|
\u
3030|
\u
24c2|
\u
d83c
[\u
dd70-
\u
dd71
]
|
\u
d83c
[\u
dd7e-
\u
dd7f
]
|
\u
d83c
\u
dd8e|
\u
d83c
[\u
dd91-
\u
dd9a
]
|
\u
d83c
[\u
dde6-
\u
ddff
]
|
[\u
d83c[
\u
de01-
\u
de02
]
|
\u
d83c
\u
de1a|
\u
d83c
\u
de2f|
[\u
d83c[
\u
de32-
\u
de3a
]
|
[\u
d83c[
\u
de50-
\u
de51
]
|
\u
203c|
\u
2049|
[\u
25aa-
\u
25ab
]
|
\u
25b6|
\u
25c0|
[\u
25fb-
\u
25fe
]
|
\u
00a9|
\u
00ae|
\u
2122|
\u
2139|
\u
d83c
\u
dc04|
[\u
2600-
\u
26FF
]
|
\u
2b05|
\u
2b06|
\u
2b07|
\u
2b1b|
\u
2b1c|
\u
2b50|
\u
2b55|
\u
231a|
\u
231b|
\u
2328|
\u
23cf|
[\u
23e9-
\u
23f3
]
|
[\u
23f8-
\u
23fa
]
|
\u
d83c
\u
dccf|
\u
2934|
\u
2935|
[\u
2190-
\u
21ff
])
/
;
const
urlRegex
=
/^https
?
:
\/\/(
www
\.)?[
-a-zA-Z0-9@:%._+~#=
]{2,256}\.[
a-z
]{2,6}\b([
-a-zA-Z0-9@:%_+.~#?&
//
=
]
*
)
$/
;
function
ChatMessage
(
props
)
{
const
handleSpawn
=
domToRender
=>
{
if
(
props
.
body
.
match
(
urlRegex
))
{
document
.
querySelector
(
"
a-scene
"
).
emit
(
"
add_media
"
,
props
.
body
);
return
;
}
const
context
=
messageCanvas
.
getContext
(
"
2d
"
);
const
emoji
=
toEmojis
(
props
.
body
);
const
isEmoji
=
emoji
.
length
===
1
&&
emoji
[
0
].
props
&&
emoji
[
0
].
props
.
children
.
match
&&
emoji
[
0
].
props
.
children
.
match
(
emojiRegex
);
const
isOneLine
=
props
.
body
.
split
(
"
\n
"
).
length
===
1
;
const
el
=
document
.
createElement
(
"
div
"
);
el
.
setAttribute
(
"
xmlns
"
,
"
http://www.w3.org/1999/xhtml
"
);
el
.
setAttribute
(
"
class
"
,
`
${
styles
.
presenceLog
}
${
styles
.
presenceLogSpawn
}
`
);
// The element is added to the DOM in order to have layout compute the width & height,
// and then it is removed after being rendered.
document
.
body
.
appendChild
(
el
);
const
entryDom
=
(
<
div
className
=
{
classNames
({
[
styles
.
presenceLogEntry
]:
!
isEmoji
,
[
styles
.
presenceLogEntryOneLine
]:
!
isEmoji
&&
isOneLine
,
[
styles
.
presenceLogEmoji
]:
isEmoji
})}
>
{
domToRender
}
<
/div
>
);
ReactDOM
.
render
(
entryDom
,
el
,
()
=>
{
// Scale by 12x
messageCanvas
.
width
=
el
.
offsetWidth
*
12
;
messageCanvas
.
height
=
el
.
offsetHeight
*
12
;
const
xhtml
=
encodeURIComponent
(
`
<svg xmlns="http://www.w3.org/2000/svg" width="
${
messageCanvas
.
width
}
" height="
${
messageCanvas
.
height
}
">
<foreignObject width="8.333%" height="8.333%" style="transform: scale(12.0);">
${
serializeElement
(
el
)}
</foreignObject>
</svg>
`
);
const
img
=
new
Image
();
img
.
onload
=
async
()
=>
{
context
.
drawImage
(
img
,
0
,
0
);
const
blob
=
await
new
Promise
(
resolve
=>
messageCanvas
.
toBlob
(
resolve
));
document
.
querySelector
(
"
a-scene
"
).
emit
(
"
add_media
"
,
new
File
([
blob
],
"
message.png
"
,
{
type
:
"
image/png
"
}));
el
.
parentNode
.
removeChild
(
el
);
};
img
.
src
=
"
data:image/svg+xml,
"
+
xhtml
;
});
};
// Support wrapping text in ` to get monospace, and multiline.
const
multiLine
=
props
.
body
.
split
(
"
\n
"
).
length
>
1
;
const
mono
=
props
.
body
.
startsWith
(
"
`
"
)
&&
props
.
body
.
endsWith
(
"
`
"
);
const
messageBodyClasses
=
{
[
styles
.
messageBody
]:
true
,
[
styles
.
messageBodyMulti
]:
multiLine
,
[
styles
.
messageBodyMono
]:
mono
};
const
body
=
(
mono
?
props
.
body
.
substring
(
1
,
props
.
body
.
length
-
1
)
:
props
.
body
).
trim
();
const
spawnableDom
=
(
<
div
className
=
{
classNames
(
messageBodyClasses
)}
>
<
Linkify
properties
=
{{
target
:
"
_blank
"
,
rel
:
"
noopener referrer
"
}}
>
{
toEmojis
(
body
)}
<
/Linkify
>
<
/div
>
);
return
(
<
div
className
=
{
props
.
className
}
>
{
props
.
maySpawn
&&
<
button
className
=
{
styles
.
spawnMessage
}
onClick
=
{()
=>
handleSpawn
(
spawnableDom
)}
/>
}
<
div
className
=
{
multiLine
?
styles
.
messageWrapMulti
:
styles
.
messageWrap
}
>
<
div
className
=
{
styles
.
messageSource
}
>
<
b
>
{
props
.
name
}
<
/b>
:
<
/div
>
{
spawnableDom
}
<
/div
>
<
/div
>
);
}
ChatMessage
.
propTypes
=
{
name
:
PropTypes
.
string
,
maySpawn
:
PropTypes
.
bool
,
body
:
PropTypes
.
string
,
className
:
PropTypes
.
string
};
import
ChatMessage
from
"
./chat-message
"
;
export
default
class
PresenceLog
extends
Component
{
static
propTypes
=
{
...
...
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