Skip to content

Commit fd4d1ee

Browse files
committed
feat(hosting): Import hosting contents.
1 parent 1bc7624 commit fd4d1ee

File tree

7 files changed

+92
-47
lines changed

7 files changed

+92
-47
lines changed

assets/src/webpage.txt.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,16 @@ async function loadDocument(url, pushHistory = true, scrollTo = true)
379379
doc.documentElement.innerHTML = html;
380380

381381
// copy document content and outline tree
382-
document.querySelector(".document-container").innerHTML = doc.querySelector(".document-container").innerHTML;
383-
document.querySelector(".outline-tree").innerHTML = doc.querySelector(".outline-tree").innerHTML;
382+
const container = doc.querySelector(".document-container");
383+
if (container) {
384+
document.querySelector(".document-container").innerHTML = container.innerHTML;
385+
document.querySelector(".outline-tree").innerHTML = doc.querySelector(".outline-tree").innerHTML;
386+
} else {
387+
console.warn('html response is not work as expected, show directly: ', url);
388+
// Error html response
389+
window.location.assign(url);
390+
return;
391+
}
384392

385393
// if the url has a heading, scroll to it
386394
let splitURL = url.split("#");

assets/webpage.txt.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/baseTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export interface RemoteItem {
128128

129129
export const COMMAND_URI = "invio";
130130
export const COMMAND_CALLBACK = "invio-auth-cb";
131+
export const COMMAND_CALLBACK_IMPORT = "invio-project-import";
131132
export const COMMAND_CALLBACK_ONEDRIVE = "invio-cb-onedrive";
132133
export const COMMAND_CALLBACK_DROPBOX = "invio-cb-dropbox";
133134

src/components/PendingStatsView.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,31 @@ export const PendingStatsViewComponent = (props: { plugin: InvioPlugin }) => {
109109
</> :
110110
null
111111
}
112-
113-
<div className={styles['subHeader']}>
114-
Local Changed Files
115-
</div>
116-
<Tree
117-
checkable
118-
showLine
119-
defaultExpandAll
120-
multiple={false}
121-
rootStyle={{
122-
background: 'black',
123-
color: 'white',
124-
paddingTop: '18px',
125-
paddingBottom: '18px',
126-
}}
127-
selectedKeys={[]}
128-
onSelect={onToRemoteSelect}
129-
onCheck={onToRemoteCheck}
130-
treeData={treeToRemoteData}
131-
/>
112+
{
113+
treeToRemoteData?.length > 0 ?
114+
<>
115+
<div className={styles['subHeader']}>
116+
Local Changed Files
117+
</div>
118+
<Tree
119+
checkable
120+
showLine
121+
defaultExpandAll
122+
multiple={false}
123+
rootStyle={{
124+
background: 'black',
125+
color: 'white',
126+
paddingTop: '18px',
127+
paddingBottom: '18px',
128+
}}
129+
selectedKeys={[]}
130+
onSelect={onToRemoteSelect}
131+
onCheck={onToRemoteCheck}
132+
treeData={treeToRemoteData}
133+
/>
134+
</> :
135+
null
136+
}
132137
<div className={styles['actions']}>
133138
<Button onClick={startSync}>Sync</Button>
134139
</div>

src/main.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
} from "./baseTypes";
2020
import {
2121
COMMAND_CALLBACK,
22+
COMMAND_CALLBACK_IMPORT,
2223
COMMAND_URI,
2324
} from "./baseTypes";
2425
import { importQrCodeUri } from "./importExport";
@@ -60,7 +61,7 @@ import Utils from './utils';
6061
import { Analytics4, loadGA } from './ga';
6162
import { TDiffType, openDiffModal } from './diff/index';
6263
import AutoUpdater from './updater'
63-
64+
import { mkdirpInVault } from "./misc";
6465
const { iconNameSyncWait, iconNameSyncPending, iconNameSyncRunning, iconNameLogs, iconNameSyncLogo } = UsingIconNames;
6566
const Menu_Tab = ` `;
6667

@@ -1184,6 +1185,47 @@ export default class InvioPlugin extends Plugin {
11841185
}
11851186
});
11861187

1188+
this.registerObsidianProtocolHandler(
1189+
COMMAND_CALLBACK_IMPORT,
1190+
async (inputParams) => {
1191+
log.info('protocol: ', COMMAND_CALLBACK_IMPORT, inputParams)
1192+
1193+
const { token, user, name, slug } = inputParams;
1194+
const dir = name
1195+
if (await app.vault.adapter.exists(dir)) {
1196+
new Notice(`Local folder ${dir} existed, import aborted`)
1197+
return;
1198+
}
1199+
if (!(dir || (typeof dir === 'string'))) {
1200+
return;
1201+
}
1202+
1203+
if (!this.settings.hostConfig) {
1204+
this.settings.hostConfig = {} as THostConfig;
1205+
}
1206+
if (token && user) {
1207+
this.settings.hostConfig.token = token;
1208+
try {
1209+
this.settings.hostConfig.user = JSON.parse(user);
1210+
} catch (error) {
1211+
log.error('parse user info failed: ', error);
1212+
this.settings.hostConfig.token = '';
1213+
this.settings.hostConfig.user = null;
1214+
}
1215+
}
1216+
await this.app.vault.adapter.mkdir(dir);
1217+
await this.switchWorkingDir(dir);
1218+
1219+
log.info('sync with remote project: ', this.settings.localWatchDir, this.settings.hostConfig);
1220+
if (this.settings.localWatchDir) {
1221+
await syncWithRemoteProject(this.settings.localWatchDir, this);
1222+
}
1223+
setTimeout(() => {
1224+
// this.syncRun('auto');
1225+
}, 100)
1226+
}
1227+
);
1228+
11871229
this.registerObsidianProtocolHandler(
11881230
COMMAND_CALLBACK,
11891231
async (inputParams) => {

src/remote.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ export class RemoteClient {
5151
throw new Error('ProjectNotSync');
5252
}
5353
if (this.hostConfig?.hostPair?.dir !== this.localWatchDir) {
54-
throw new Error('NeedSwitchProject');
54+
throw new Error(`NeedSwitchProject: ${this.localWatchDir} to ${this.hostConfig?.hostPair?.dir}`);
5555
}
56-
return (this.hostConfig?.hostPair?.password ? 'p/' : '') + this.hostConfig?.hostPair.slug;
56+
return this.hostConfig?.hostPair.slug;
5757
}
5858

5959
getUseHostDirname() {
@@ -68,7 +68,7 @@ export class RemoteClient {
6868
throw new Error('ProjectNotSync');
6969
}
7070
if (this.hostConfig?.hostPair?.dir !== this.localWatchDir) {
71-
throw new Error('NeedSwitchProject');
71+
throw new Error(`NeedSwitchProject: ${this.localWatchDir} to ${this.hostConfig?.hostPair?.dir}`);
7272
}
7373
return this.hostConfig?.hostPair.dir;
7474
}
@@ -82,13 +82,8 @@ export class RemoteClient {
8282
const paths = Path.splitString(key);
8383
if (paths?.length > 0) {
8484
let dir = hasPrefix ? paths[1] : paths[0];
85-
if (dir === 'p') {
86-
paths.splice(0, 1);
87-
dir = hasPrefix ? paths[1] : paths[0];
88-
}
89-
9085
if (dir !== this.localWatchDir) {
91-
throw new Error('NeedSwitchProject');
86+
throw new Error(`NeedSwitchProject: ${this.localWatchDir} to ${dir}`);
9287
}
9388
if (hasPrefix) {
9489
paths[1] = this.getUseHostSlug();
@@ -109,21 +104,16 @@ export class RemoteClient {
109104
} else {
110105
const hasPrefix = slug?.startsWith(RemoteSrcPrefix);
111106
const paths = slug?.split(WEB_PATH_SPLITER);
112-
let encrypted = false;
113107
if (paths?.length > 0) {
114108
let dir = hasPrefix ? paths[1] : paths[0];
115-
if (dir === 'p') {
116-
encrypted = true;
117-
dir = hasPrefix ? paths[2] : paths[1];
118-
}
119109
const getSlug = this.getUseHostSlug();
120-
if (dir !== getSlug?.replace(/^p\//, '')) {
121-
throw new Error('NeedSwitchProject');
110+
if (dir !== getSlug) {
111+
throw new Error(`NeedSwitchProject: ${getSlug} to ${dir}`);
122112
}
123113
if (hasPrefix) {
124-
paths[encrypted ? 2 : 1] = this.getUseHostDirname();
114+
paths[1] = this.getUseHostDirname();
125115
} else {
126-
paths[encrypted ? 1 : 0] = this.getUseHostDirname();
116+
paths[0] = this.getUseHostDirname();
127117
}
128118
webPath = Path.joinString(paths);
129119
log.info('get local path: ', webPath)

src/sync.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ export const fetchRemoteFileMD = async (
359359
if (buf?.deleted) {
360360
return buf;
361361
}
362-
const resp: RemoteItem = await client.getRemoteMeta(RemoteSrcPrefix + remoteFilePath)
363-
362+
const resp: RemoteItem = await client.getRemoteMeta(RemoteSrcPrefix + client.getUseHostSlugPath(remoteFilePath))
364363
let data;
365364
if (typeof buf === "string") {
366365
data = buf;
@@ -447,7 +446,7 @@ export const pruneTouchedFiles = async (vault: Vault, client: RemoteClient, list
447446
client,
448447
vault,
449448
).catch(err => {
450-
log.info('remote empty ', err);
449+
log.info('remote empty uploadLocalToRemote ', item.key, err);
451450
return null;
452451
})
453452
log.info('remote md: ', remoteMD);
@@ -468,10 +467,9 @@ export const pruneTouchedFiles = async (vault: Vault, client: RemoteClient, list
468467
client,
469468
vault,
470469
).catch(err => {
471-
log.info('remote empty ', err);
470+
log.info('remote empty ', item.key, err);
472471
return null;
473472
})
474-
log.info('remote md: ', remoteMD);
475473
if (!remoteMD || !(await isRemoteFileDiff(vault, item.key, remoteMD.data))) {
476474
log.info('file contents diff nothing', attr, item.key)
477475
list[attr] = null;
@@ -482,6 +480,7 @@ export const pruneTouchedFiles = async (vault: Vault, client: RemoteClient, list
482480
})
483481
const result = await Promise.all(promises);
484482
log.info('list result: ', result);
483+
return result;
485484
}
486485

487486
const ensembleMixedStates = async (

0 commit comments

Comments
 (0)