Skip to content

Commit 3d870f9

Browse files
committed
feat: add @rollup/plugin-inject for global variable injection and update imports for @hackmd/ot
1 parent 5f9387c commit 3d870f9

File tree

5 files changed

+175
-6
lines changed

5 files changed

+175
-6
lines changed

package-lock.json

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"@hackmd/emojify.js": "^2.1.0",
113113
"@hackmd/idle-js": "~1.0.1",
114114
"@hackmd/js-sequence-diagrams": "~0.0.1-alpha.3",
115+
"@rollup/plugin-inject": "^5.0.5",
115116
"@susisu/mte-kernel": "^2.1.0",
116117
"@vitejs/plugin-legacy": "^6.0.2",
117118
"@viz-js/viz": "^3.12.0",

patches/@hackmd+ot+0.1.0.patch

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
diff --git a/node_modules/@hackmd/ot/lib/client.js b/node_modules/@hackmd/ot/lib/client.js
2+
index 7ee19fd..8dfc6c6 100644
3+
--- a/node_modules/@hackmd/ot/lib/client.js
4+
+++ b/node_modules/@hackmd/ot/lib/client.js
5+
@@ -1,9 +1,5 @@
6+
// translation of https://github.com/djspiewak/cccp/blob/master/agent/src/main/scala/com/codecommit/cccp/agent/state.scala
7+
8+
-if (typeof ot === 'undefined') {
9+
- var ot = {};
10+
-}
11+
-
12+
ot.Client = (function (global) {
13+
'use strict';
14+
15+
diff --git a/node_modules/@hackmd/ot/lib/editor-client.js b/node_modules/@hackmd/ot/lib/editor-client.js
16+
index 6e57349..a2d6632 100644
17+
--- a/node_modules/@hackmd/ot/lib/editor-client.js
18+
+++ b/node_modules/@hackmd/ot/lib/editor-client.js
19+
@@ -356,3 +356,9 @@ ot.EditorClient = (function () {
20+
21+
return EditorClient;
22+
}());
23+
+
24+
+
25+
+if (typeof module === 'object') {
26+
+ module.exports = ot.EditorClient;
27+
+}
28+
+
129
diff --git a/node_modules/@hackmd/ot/lib/index.js b/node_modules/@hackmd/ot/lib/index.js
230
index 0b16a6b..ad64c70 100644
331
--- a/node_modules/@hackmd/ot/lib/index.js
@@ -9,3 +37,80 @@ index 0b16a6b..ad64c70 100644
937
-exports.scriptsDir = require("path").resolve(__dirname, "../") + "/dist";
1038
+// exports.scriptsDir = require("path").resolve(__dirname, "../") + "/dist";
1139

40+
diff --git a/node_modules/@hackmd/ot/lib/selection.js b/node_modules/@hackmd/ot/lib/selection.js
41+
index 72bf8bd..a5ddafa 100644
42+
--- a/node_modules/@hackmd/ot/lib/selection.js
43+
+++ b/node_modules/@hackmd/ot/lib/selection.js
44+
@@ -1,8 +1,3 @@
45+
-if (typeof ot === 'undefined') {
46+
- // Export for browsers
47+
- var ot = {};
48+
-}
49+
-
50+
ot.Selection = (function (global) {
51+
'use strict';
52+
53+
diff --git a/node_modules/@hackmd/ot/lib/server.js b/node_modules/@hackmd/ot/lib/server.js
54+
index 9988a34..bd89164 100644
55+
--- a/node_modules/@hackmd/ot/lib/server.js
56+
+++ b/node_modules/@hackmd/ot/lib/server.js
57+
@@ -1,7 +1,3 @@
58+
-if (typeof ot === 'undefined') {
59+
- var ot = {};
60+
-}
61+
-
62+
ot.Server = (function (global) {
63+
'use strict';
64+
65+
diff --git a/node_modules/@hackmd/ot/lib/simple-text-operation.js b/node_modules/@hackmd/ot/lib/simple-text-operation.js
66+
index 6db296e..592eee1 100644
67+
--- a/node_modules/@hackmd/ot/lib/simple-text-operation.js
68+
+++ b/node_modules/@hackmd/ot/lib/simple-text-operation.js
69+
@@ -1,8 +1,3 @@
70+
-if (typeof ot === 'undefined') {
71+
- // Export for browsers
72+
- var ot = {};
73+
-}
74+
-
75+
ot.SimpleTextOperation = (function (global) {
76+
77+
var TextOperation = global.ot ? global.ot.TextOperation : require('./text-operation');
78+
diff --git a/node_modules/@hackmd/ot/lib/text-operation.js b/node_modules/@hackmd/ot/lib/text-operation.js
79+
index d546849..7d793dc 100644
80+
--- a/node_modules/@hackmd/ot/lib/text-operation.js
81+
+++ b/node_modules/@hackmd/ot/lib/text-operation.js
82+
@@ -1,8 +1,3 @@
83+
-if (typeof ot === 'undefined') {
84+
- // Export for browsers
85+
- var ot = {};
86+
-}
87+
-
88+
ot.TextOperation = (function () {
89+
'use strict';
90+
91+
diff --git a/node_modules/@hackmd/ot/lib/undo-manager.js b/node_modules/@hackmd/ot/lib/undo-manager.js
92+
index 19e89f1..96eda4a 100644
93+
--- a/node_modules/@hackmd/ot/lib/undo-manager.js
94+
+++ b/node_modules/@hackmd/ot/lib/undo-manager.js
95+
@@ -1,8 +1,3 @@
96+
-if (typeof ot === 'undefined') {
97+
- // Export for browsers
98+
- var ot = {};
99+
-}
100+
-
101+
ot.UndoManager = (function () {
102+
'use strict';
103+
104+
diff --git a/node_modules/@hackmd/ot/lib/wrapped-operation.js b/node_modules/@hackmd/ot/lib/wrapped-operation.js
105+
index 91050f4..8dd5dd5 100644
106+
--- a/node_modules/@hackmd/ot/lib/wrapped-operation.js
107+
+++ b/node_modules/@hackmd/ot/lib/wrapped-operation.js
108+
@@ -1,8 +1,3 @@
109+
-if (typeof ot === 'undefined') {
110+
- // Export for browsers
111+
- var ot = {};
112+
-}
113+
-
114+
ot.WrappedOperation = (function (global) {
115+
'use strict';
116+

public/js/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@ import '@vendor/inlineAttachment/inline-attachment'
99
import '@vendor/inlineAttachment/codemirror.inline-attachment'
1010
import Visibility from 'visibilityjs'
1111
// Import the original vendor file
12-
import ot from '@hackmd/ot'
13-
import $ from 'jquery'
12+
13+
import '@hackmd/ot'
14+
import '@hackmd/ot/lib/socketio-adapter'
15+
import '@hackmd/ot/lib/codemirror-adapter'
16+
import '@hackmd/ot/lib/undo-manager'
17+
// import '@hackmd/ot/lib/client'
18+
import '@hackmd/ot/lib/editor-client'
19+
20+
import mousewhell from 'jquery-mousewheel'
1421
import '@vendor/jquery-textcomplete/jquery.textcomplete'
22+
import '@vendor/jquery-ui/jquery-ui.min.js'
23+
import '@vendor/bootstrap/tooltip.min.js'
24+
import '@vendor/showup/showup.js'
1525

1626
import { saveAs } from 'file-saver'
1727
import randomColor from 'randomcolor'
@@ -131,6 +141,11 @@ import '@css/extra.css'
131141
import '@css/site.css'
132142
import 'spin.js/spin.css'
133143

144+
mousewhell($)
145+
146+
// ot.Client = Client
147+
// ot.EditorClient = EditorClient
148+
134149
// require('highlight.js/styles/github-gist.css') // Duplicate, removed
135150

136151
var defaultTextHeight = 20

vite.config.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import copy from 'rollup-plugin-copy'
44
import EnvironmentPlugin from 'vite-plugin-environment'
55
import string from 'vite-plugin-string' // Import string plugin
66
import viteCommonjs from 'vite-plugin-commonjs' // Import commonjs plugin
7+
import inject from "@rollup/plugin-inject"
78
import path from 'path'
89

910
// https://vitejs.dev/config/
@@ -13,6 +14,16 @@ export default defineConfig({
1314
legacy({
1415
targets: ['defaults', 'not IE 11'] // Similar target as babel-preset-env might use
1516
}),
17+
inject({
18+
// Inject global variables for legacy builds
19+
$: 'jquery',
20+
jQuery: 'jquery',
21+
'window.jQuery': 'jquery',
22+
'window.$': 'jquery',
23+
key: 'keymaster',
24+
'window.key': 'keymaster',
25+
// Add any other globals you need to inject
26+
}),
1627
string({ // Add string plugin
1728
include: '**/*.html' // Load all .html files as strings
1829
}),
@@ -126,7 +137,8 @@ export default defineConfig({
126137
define: {
127138
// Example: 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
128139
__dirname: '""', // Define __dirname as empty string for browser compatibility
129-
global: 'globalThis' // Explicitly define global for Vite
140+
global: 'globalThis', // Explicitly define global for Vite
141+
ot: {}, // Define ot as empty object for browser compatibility
130142
},
131143
root: __dirname,
132144
base: '/',
@@ -146,9 +158,10 @@ export default defineConfig({
146158
'@hackmd/idle-js',
147159
'spin.js',
148160
// Explicitly include the OT files being imported
149-
'@hackmd/ot/lib/editor-client.js',
150-
'@hackmd/ot/lib/socketio-adapter.js',
151-
'@hackmd/ot/lib/codemirror-adapter.js'
161+
// '@hackmd/ot/lib/client.js',
162+
// '@hackmd/ot/lib/editor-client.js',
163+
// '@hackmd/ot/lib/socketio-adapter.js',
164+
// '@hackmd/ot/lib/codemirror-adapter.js'
152165
]
153166
},
154167
server: {

0 commit comments

Comments
 (0)