Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
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
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
10f6ab36
Commit
10f6ab36
authored
Aug 25, 2020
by
Lyza Danger Gardner
Committed by
Lyza Gardner
Aug 26, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Split out config proper from script-embed script
- Allow PDFs to benefit from shared/easily-updated client config
parent
da9c1d78
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
32 deletions
+32
-32
serve-dev.js
dev-server/serve-dev.js
+30
-14
client-config.js.mustache
dev-server/templates/client-config.js.mustache
+0
-4
pdfjs-viewer.mustache
dev-server/templates/pdfjs-viewer.mustache
+1
-7
generate-pdfjs-template.py
scripts/generate-pdfjs-template.py
+1
-7
No files found.
dev-server/serve-dev.js
View file @
10f6ab36
...
...
@@ -21,28 +21,40 @@ const TEMPLATE_PATH = `${__dirname}/templates/`;
*/
/**
*
Generate `<script>` content for client configuration and injection
*
Render client config and script embed
*
* @param {string} clientUrl
* @return {string}
* @param {Object} context
*/
function
renderConfig
(
clientUrl
)
{
const
scriptTemplate
=
fs
.
readFileSync
(
`
${
TEMPLATE_PATH
}
client-config.js.mustache`
,
'utf-8'
);
return
Mustache
.
render
(
scriptTemplate
,
{
clientUrl
});
function
renderScript
(
context
)
{
const
scriptTemplate
=
`
{{{hypothesisConfig}}}
<script>
const embedScript = document.createElement('script');
embedScript.src = '{{{clientUrl}}}'.replace('{current_host}', document.location.hostname);
document.body.appendChild(embedScript);
</script>
`
;
return
Mustache
.
render
(
scriptTemplate
,
context
);
}
/**
* Build context for rendering templates in the defined views directory. This
* is dead simple at present but could be extended as needs grow.
* Build context for rendering templates in the defined views directory.
*
* @param {Config} config
*/
function
templateContext
(
config
)
{
// Just the config by itself, in contrast with `hypothesisScript`, which
// combines this config with a <script> that adds the embed script
const
hypothesisConfig
=
fs
.
readFileSync
(
`
${
TEMPLATE_PATH
}
client-config.js.mustache`
,
'utf-8'
);
return
{
hypothesisScript
:
renderConfig
(
config
.
clientUrl
),
hypothesisConfig
,
hypothesisScript
:
renderScript
({
hypothesisConfig
,
clientUrl
:
config
.
clientUrl
,
}),
};
}
...
...
@@ -89,11 +101,15 @@ function serveDev(port, config) {
// Serve PDF documents with PDFJS viewer and client script
app
.
get
(
'/pdf/:pdf'
,
(
req
,
res
,
next
)
=>
{
if
(
fs
.
existsSync
(
`
${
PDF_PATH
}${
req
.
params
.
pdf
}
.pdf`
))
{
const
relativeSourceUrl
=
`/pdf-source/
${
req
.
params
.
pdf
}
.pdf`
;
const
fullUrl
=
`
${
req
.
protocol
}
://
${
req
.
hostname
}
:
${
port
}${
req
.
originalUrl
}
`
;
const
context
=
templateContext
(
config
);
res
.
render
(
'pdfjs-viewer'
,
{
...
context
,
clientUrl
:
config
.
clientUrl
,
// URL to embed source
documentUrl
:
fullUrl
,
// The URL that annotations are associated with
url
:
`/pdf-source/
${
req
.
params
.
pdf
}
.pdf`
,
// The URL for the PDF source file
clientUrl
:
config
.
clientUrl
,
url
:
relativeSourceUrl
,
// The URL for the PDF source file
});
}
else
{
next
();
...
...
dev-server/templates/client-config.js.mustache
View file @
10f6ab36
<script>
var
appHost
=
document
.
location
.
hostname
;
window
.
hypothesisConfig
=
function
()
{
return
{
// enableExperimentalNewNoteButton: true,
...
...
@@ -25,7 +24,4 @@
openSidebar
:
true
,
};
};
var
embedScript
=
document
.
createElement
(
'script'
);
embedScript
.
src
=
'
{{{
clientUrl
}}}
'
.
replace
(
'{current_host}'
,
document
.
location
.
hostname
);
document
.
body
.
appendChild
(
embedScript
);
</script>
\ No newline at end of file
dev-server/templates/pdfjs-viewer.mustache
View file @
10f6ab36
...
...
@@ -49,13 +49,7 @@ window.CLIENT_URL = '{{{clientUrl}}}'.replace('{current_host}', document.locatio
<script
src=
"/scripts/pdfjs-init.js"
></script>
<!-- Configure Hypothesis client. -->
<script>
window
.
hypothesisConfig
=
function
()
{
return
{
openSidebar
:
true
,
};
};
</script>
{{{
hypothesisConfig
}}}
<!-- End Hypothesis modifications -->
...
...
scripts/generate-pdfjs-template.py
View file @
10f6ab36
...
...
@@ -48,13 +48,7 @@ window.CLIENT_URL = '{{{clientUrl}}}'.replace('{current_host}', document.locatio
<script src="/scripts/pdfjs-init.js"></script>
<!-- Configure Hypothesis client. -->
<script>
window.hypothesisConfig = function() {
return {
openSidebar: true,
};
};
</script>
{{{hypothesisConfig}}}
<!-- End Hypothesis modifications -->
"""
...
...
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