Skip to content

Commit d75f237

Browse files
committed
Fix input autoresize functionality
1 parent e17460a commit d75f237

File tree

10 files changed

+74
-14
lines changed

10 files changed

+74
-14
lines changed

R/chat.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ chat_deps <- function() {
1313
"shinychat",
1414
utils::packageVersion("shinychat"),
1515
package = "shinychat",
16-
src = "chat",
17-
script = list(src = "chat.js", type = "module"),
18-
stylesheet = "chat.css"
16+
src = "lib/shiny",
17+
script = list(
18+
list(src = "chat/chat.js", type = "module"),
19+
list(src = "text-area/textarea-autoresize.js", type = "module")
20+
),
21+
stylesheet = c("chat/chat.css", "text-area/textarea-autoresize.css")
1922
)
2023
}
2124

inst/chat/GIT_VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

inst/lib/shiny/GIT_VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
005b556bbcec0c5d50a96df5128771aedcf5f0cc
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* text-area/textarea-autoresize.css */
2+
.textarea-autoresize textarea.form-control {
3+
padding: 5px 8px;
4+
resize: none;
5+
overflow-y: hidden;
6+
height: auto;
7+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// text-area/textarea-autoresize.ts
2+
function onDelegatedEvent(eventName, selector, callback) {
3+
document.addEventListener(eventName, (e) => {
4+
const e2 = e;
5+
if (e2.target.matches(selector)) {
6+
callback(e2.target);
7+
}
8+
});
9+
}
10+
var textAreaIntersectionObserver = null;
11+
function callUpdateHeightWhenTargetIsVisible(target) {
12+
if (textAreaIntersectionObserver === null) {
13+
textAreaIntersectionObserver = new IntersectionObserver(
14+
(entries, observer) => {
15+
entries.forEach((entry) => {
16+
if (!entry.isIntersecting) {
17+
return;
18+
}
19+
textAreaIntersectionObserver.unobserve(entry.target);
20+
update_height(entry.target);
21+
});
22+
}
23+
);
24+
}
25+
textAreaIntersectionObserver.observe(target);
26+
}
27+
function update_height(target) {
28+
if (target.scrollHeight > 0) {
29+
target.style.height = "auto";
30+
target.style.height = target.scrollHeight + "px";
31+
} else {
32+
callUpdateHeightWhenTargetIsVisible(target);
33+
}
34+
}
35+
onDelegatedEvent(
36+
"input",
37+
"textarea.textarea-autoresize",
38+
(target) => {
39+
update_height(target);
40+
}
41+
);
42+
function update_on_load() {
43+
if (document.readyState === "loading") {
44+
setTimeout(update_on_load, 10);
45+
return;
46+
}
47+
const textAreas = document.querySelectorAll(
48+
"textarea.textarea-autoresize"
49+
);
50+
textAreas.forEach(update_height);
51+
}
52+
update_on_load();

scripts/update-chat.sh

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env bash
22

33
set -e
4+
set -x
45

56
# This script downloads new chat CSS/JS assets from py-shiny, and stamps the
67
# directory with a GIT_VERSION.
78

89
REPO_URL="https://github.com/posit-dev/py-shiny.git"
9-
DIRECTORY="shiny/www/py-shiny/chat"
1010
BRANCH=chat-append-incremental
11+
DEST_DIR="inst/lib/shiny"
1112

1213
if [ ! -f "shinychat.Rproj" ]; then
1314
echo "Error: You must execute this script from the repo root (./scripts/update-chat.sh)."
@@ -17,12 +18,9 @@ fi
1718
# Clone the repository with sparse-checkout enabled
1819
git clone -b "$BRANCH" --depth 1 "$REPO_URL" repo_tmp
1920

20-
# Conservatively delete the existing
21-
rm -f inst/chat/GIT_VERSION inst/chat/chat.css inst/chat/chat.css.map inst/chat/chat.js inst/chat/chat.js
22-
if [ -d chat ]; then
23-
rm -r chat
24-
fi
25-
26-
cp -R "repo_tmp/$DIRECTORY" ./inst/
27-
(cd repo_tmp && git rev-parse HEAD > ../inst/chat/GIT_VERSION)
28-
rm -rf repo_tmp
21+
rm -rf "$DEST_DIR"
22+
mkdir -p "$DEST_DIR"
23+
cp -R "repo_tmp/shiny/www/py-shiny/chat" "$DEST_DIR/chat"
24+
cp -R "repo_tmp/shiny/www/py-shiny/text-area" "$DEST_DIR/text-area"
25+
(cd repo_tmp; git rev-parse HEAD) > "${DEST_DIR}/GIT_VERSION"
26+
rm -rf repo_tmp

0 commit comments

Comments
 (0)