FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
wiki-plugin-code/src/client/code.js at main · fedwiki/wiki-plugin-code · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
fedwiki
/
wiki-plugin-code
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
wiki-plugin-code
/
src
/
client
/
code.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
119 lines (104 loc) · 3.69 KB
Breadcrumbs
wiki-plugin-code
/
src
/
client
/
code.js
Copy path
File metadata and controls
119 lines (104 loc) · 3.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Federated Wiki : Code Plugin
*
* Licensed under the MIT license.
* https://github.com/fedwiki/wiki-plugin-code/blob/master/LICENSE.txt
*/
import
hljs
from
'highlight.js'
const
emit
=
async
(
$item
,
item
)
=>
{
if
(
!
$
(
"link[href='/plugins/code/highlight.css']"
)
.
length
)
{
$
(
'<link rel="stylesheet" href="/plugins/code/highlight.css">'
)
.
appendTo
(
'head'
)
}
if
(
!
$
(
"link[href='/plugins/code/code.css']"
)
.
length
)
{
$
(
'<link rel="stylesheet" href="/plugins/code/code.css">'
)
.
appendTo
(
'head'
)
}
let
autoLanguage
const
extractLanguage
=
text
=>
{
const
{
language
:
autoLanguage
,
value
}
=
hljs
.
highlightAuto
(
text
)
console
.
log
(
'using detacted language:'
,
autoLanguage
)
return
value
}
const
highlight
=
(
)
=>
{
let
value
let
output
try
{
output
=
hljs
.
highlight
(
item
.
text
,
{
language
:
item
.
language
}
)
value
=
output
.
value
}
catch
(
error
)
{
output
=
hljs
.
highlightAuto
(
item
.
text
)
value
=
output
.
value
console
.
log
(
'Hightlight Error:'
,
error
.
message
,
'using detected language:'
,
output
.
language
)
}
return
value
}
$item
.
html
(
`<pre class='hljs'><code class='hljs'>
${
item
.
language
?
highlight
(
)
:
extractLanguage
(
item
.
text
)
}
</code></pre>`
,
)
}
const
bind
=
(
$item
,
item
)
=>
{
// $item.on('dblclick', () => wiki.textEditor($item, item))
$item
.
on
(
'dblclick'
,
(
)
=>
editor
(
$item
,
item
)
)
}
const
editor
=
async
(
$item
,
item
)
=>
{
if
(
!
$
(
'.editEnable'
)
.
is
(
':visible'
)
)
return
if
(
$item
.
hasClass
(
'textEditing'
)
)
return
const
keydownHandler
=
e
=>
{
// esc, or ctrl-s, or meta-s for save
if
(
e
.
which
===
27
||
(
(
e
.
ctrlKey
||
e
.
metaKey
)
&&
e
.
which
===
83
)
)
{
e
.
preventDefault
(
)
$item
.
trigger
(
'focusout'
)
return
false
}
// ctrl-i, or meta-i, for information
if
(
(
e
.
ctrlKey
||
e
.
metaKey
)
&&
e
.
which
===
73
)
{
e
.
preventDefault
(
)
const
page
=
e
.
shiftKey
?
undefined
:
$
(
e
.
target
)
.
parents
(
'.page'
)
wiki
.
doInternalLink
(
'about code plugin'
,
page
)
return
false
}
}
const
pointerdownHandler
=
event
=>
{
const
isStillInside
=
$item
[
0
]
.
contains
(
event
.
target
)
if
(
!
isStillInside
)
{
$
(
document
)
.
off
(
'pointerdown'
,
pointerdownHandler
)
$item
.
removeClass
(
'textEditing'
)
$codeEditor
.
off
(
)
const
$page
=
$item
.
parents
(
'.page:first'
)
if
(
$item
.
find
(
'textarea'
)
.
val
(
)
.
length
>
0
)
{
const
languageChanged
=
item
.
language
!==
$item
.
find
(
'#code-language'
)
.
val
(
)
const
codeChanged
=
item
.
text
!==
$item
.
find
(
'textarea'
)
.
val
(
)
item
.
text
=
$item
.
find
(
'textarea'
)
.
val
(
)
item
.
language
=
$item
.
find
(
'#code-language'
)
.
val
(
)
wiki
.
doPlugin
(
$item
.
empty
(
)
,
item
)
if
(
item
.
language
===
original
.
language
&&
item
.
text
===
original
.
text
)
return
wiki
.
pageHandler
.
put
(
$page
,
{
type
:
'edit'
,
id
:
item
.
id
,
item
:
item
}
)
}
else
{
wiki
.
pageHandler
.
put
(
$page
,
{
type
:
'remove'
,
id
:
item
.
id
}
)
const
index
=
$
(
'.item'
)
.
index
(
$item
)
$item
.
remove
(
)
wiki
.
renderFrom
(
index
)
}
}
return
}
$item
.
addClass
(
'textEditing'
)
$item
.
off
(
)
const
original
=
{
text
:
item
.
text
||
''
,
language
:
item
.
language
||
''
,
}
const
$codeEditor
=
$
(
`
<textarea>
${
item
.
text
||
''
}
</textarea>`
)
$item
.
html
(
$codeEditor
)
$item
.
append
(
`<div id='code-options'></div>`
)
$
(
'#code-options'
)
.
append
(
`
<div>
<label>Language:</label>
<input list="languages" id="code-language" name="language-choice"
${
item
.
language
?
`value="
${
item
.
language
}
"`
:
`placeholder="Code Language"`
}
>
</div>`
)
$item
.
on
(
'keydown'
,
keydownHandler
)
$
(
document
)
.
on
(
'pointerdown'
,
pointerdownHandler
)
}
if
(
typeof
window
!==
'undefined'
&&
window
!==
null
)
{
window
.
plugins
.
code
=
{
emit
,
bind
,
editor
}
}
Back
|
FazBrowse Home
|
New Git URL