From 2d773dd2d089425f8efc5e4191cf51d4c9852846 Mon Sep 17 00:00:00 2001 From: xinminsu Date: Thu, 16 Jun 2022 20:41:37 +0800 Subject: [PATCH 01/14] add ipfs menu --- src/main/menu/templates/file.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/menu/templates/file.js b/src/main/menu/templates/file.js index b62ac0c3..bd833c02 100755 --- a/src/main/menu/templates/file.js +++ b/src/main/menu/templates/file.js @@ -33,6 +33,12 @@ export default function (keybindings, userPreference, recentlyUsedFiles) { click (menuItem, browserWindow) { actions.openFolder(browserWindow) } + }, { + label: 'Open File From Ipfs', + accelerator: keybindings.getAccelerator('file.open-file'), + click (menuItem, browserWindow) { + actions.openFile(browserWindow) + } }] } @@ -87,6 +93,12 @@ export default function (keybindings, userPreference, recentlyUsedFiles) { click (menuItem, browserWindow) { actions.saveAs(browserWindow) } + }, { + label: 'Save to Ipfs', + accelerator: keybindings.getAccelerator('file.save'), + click (menuItem, browserWindow) { + actions.save(browserWindow) + } }, { label: 'Auto Save', type: 'checkbox', From 27b374cf49d19a8c1f146fae18ef2238244a6db3 Mon Sep 17 00:00:00 2001 From: xinminsu Date: Tue, 5 Jul 2022 18:33:21 +0800 Subject: [PATCH 02/14] add ipfs dependences --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index fc040fd2..bb53052b 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,10 @@ "vue-electron": "^1.0.6", "vue-router": "^3.5.3", "vuex": "^3.6.2", - "webfontloader": "^1.6.28" + "webfontloader": "^1.6.28", + "ipfs-core": "^0.15.2", + "it-all": "^1.0.4", + "uint8arrays": "^3.0.0" }, "devDependencies": { "@babel/core": "^7.17.7", From 80090912b6adc07f8d85c85ef03d723d4d5e66e8 Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 6 Jul 2022 19:20:42 +0800 Subject: [PATCH 03/14] add save to ipfs --- src/main/menu/actions/file.js | 60 +++++++++++++++++++++++++++++++++ src/main/menu/templates/file.js | 2 +- src/renderer/store/editor.js | 18 ++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/main/menu/actions/file.js b/src/main/menu/actions/file.js index b743735a..01cb8fee 100644 --- a/src/main/menu/actions/file.js +++ b/src/main/menu/actions/file.js @@ -158,6 +158,58 @@ const handleResponseForSave = async (e, { id, filename, markdown, pathname, opti }) } +const handleResponseForSaveToIpfs = async (e, { id, filename, markdown, pathname, options, defaultPath }) => { + const win = BrowserWindow.fromWebContents(e.sender) + let recommendFilename = getRecommendTitleFromMarkdownString(markdown) + if (!recommendFilename) { + recommendFilename = filename || 'Untitled' + } + + // If the file doesn't exist on disk add it to the recently used documents later + // and execute file from filesystem watcher for a short time. The file may exists + // on disk nevertheless but is already tracked by MarkText. + const alreadyExistOnDisk = !!pathname + + let filePath = pathname + + if (!filePath) { + const { filePath: dialogPath, canceled } = await dialog.showSaveDialog(win, { + defaultPath: path.join(defaultPath || getPath('documents'), `${recommendFilename}.md`) + }) + + if (dialogPath && !canceled) { + filePath = dialogPath + } + } + + // Save dialog canceled by user - no error. + if (!filePath) { + return Promise.resolve() + } + + filePath = path.resolve(filePath) + const extension = path.extname(filePath) || '.md' + filePath = !filePath.endsWith(extension) ? filePath += extension : filePath + return writeMarkdownFile(filePath, markdown, options, win) + .then(() => { + if (!alreadyExistOnDisk) { + ipcMain.emit('window-add-file-path', win.id, filePath) + ipcMain.emit('menu-add-recently-used', filePath) + + const filename = path.basename(filePath) + win.webContents.send('mt::set-pathname', { id, pathname: filePath, filename }) + } else { + ipcMain.emit('window-file-saved', win.id, filePath) + win.webContents.send('mt::tab-saved', id) + } + return id + }) + .catch(err => { + log.error('Error while saving:', err) + win.webContents.send('mt::tab-save-failure', id, err.message) + }) +} + const showUnsavedFilesMessage = async (win, files) => { const { response } = await dialog.showMessageBox(win, { type: 'warning', @@ -317,6 +369,8 @@ ipcMain.on('mt::close-window-confirm', async (e, unsavedFiles) => { ipcMain.on('mt::response-file-save', handleResponseForSave) +ipcMain.on('mt::response-file-save-to-ipfs', handleResponseForSaveToIpfs) + ipcMain.on('mt::response-export', handleResponseForExport) ipcMain.on('mt::response-print', handleResponseForPrint) @@ -574,6 +628,12 @@ export const save = win => { } } +export const saveToIpfs = win => { + if (win && win.webContents) { + win.webContents.send('mt::editor-ask-file-save-to-ipfs') + } +} + export const saveAs = win => { if (win && win.webContents) { win.webContents.send('mt::editor-ask-file-save-as') diff --git a/src/main/menu/templates/file.js b/src/main/menu/templates/file.js index bd833c02..0e7de3b6 100755 --- a/src/main/menu/templates/file.js +++ b/src/main/menu/templates/file.js @@ -97,7 +97,7 @@ export default function (keybindings, userPreference, recentlyUsedFiles) { label: 'Save to Ipfs', accelerator: keybindings.getAccelerator('file.save'), click (menuItem, browserWindow) { - actions.save(browserWindow) + actions.saveToIpfs(browserWindow) } }, { label: 'Auto Save', diff --git a/src/renderer/store/editor.js b/src/renderer/store/editor.js index 0e407cef..a68b5d0b 100644 --- a/src/renderer/store/editor.js +++ b/src/renderer/store/editor.js @@ -430,6 +430,24 @@ const actions = { }) }, + LISTEN_FOR_SAVE_TO_IPFS ({ state, rootState }) { + ipcRenderer.on('mt::editor-ask-file-save-to-ipfs', () => { + const { id, filename, pathname, markdown } = state.currentFile + const options = getOptionsFromState(state.currentFile) + const defaultPath = getRootFolderFromState(rootState) + if (id) { + ipcRenderer.send('mt::response-file-save-to-ipfs', { + id, + filename, + pathname, + markdown, + options, + defaultPath + }) + } + }) + }, + // need pass some data to main process when `save as` menu item clicked LISTEN_FOR_SAVE_AS ({ state, rootState }) { ipcRenderer.on('mt::editor-ask-file-save-as', () => { From 0cac5b088e1cdd00520ba088c1b4bae82cf30798 Mon Sep 17 00:00:00 2001 From: "OEM Configuration (temporary user)" Date: Sat, 6 Aug 2022 12:30:29 +0800 Subject: [PATCH 04/14] add save to ipfs function. add save to ipfs function. --- src/renderer/commands/descriptions.js | 1 + src/renderer/commands/index.js | 5 +++++ src/renderer/pages/app.vue | 1 + 3 files changed, 7 insertions(+) diff --git a/src/renderer/commands/descriptions.js b/src/renderer/commands/descriptions.js index 4a58e0d3..a7f2f745 100644 --- a/src/renderer/commands/descriptions.js +++ b/src/renderer/commands/descriptions.js @@ -11,6 +11,7 @@ const commandDescriptions = Object.freeze({ 'file.open-folder': 'File: Open Folder', 'file.save': 'File: Save', 'file.save-as': 'File: Save As...', + 'file.save-to-ipfs': 'File: Save to Ipfs...', 'file.move-file': 'File: Move...', 'file.rename-file': 'File: Rename...', 'file.quick-open': 'File: Show quick open dialog', diff --git a/src/renderer/commands/index.js b/src/renderer/commands/index.js index 1c9b15d1..5042c60d 100644 --- a/src/renderer/commands/index.js +++ b/src/renderer/commands/index.js @@ -63,6 +63,11 @@ const commands = [ execute: async () => { ipcRenderer.emit('mt::editor-ask-file-save', null) } + }, { + id: 'file.save-to-ipfs', + execute: async () => { + ipcRenderer.emit('mt::editor-ask-file-save-to-ipfs', null) + } }, { id: 'file.save-as', execute: async () => { diff --git a/src/renderer/pages/app.vue b/src/renderer/pages/app.vue index cd1784a7..946a219e 100644 --- a/src/renderer/pages/app.vue +++ b/src/renderer/pages/app.vue @@ -144,6 +144,7 @@ export default { dispatch('LISTEN_FOR_SAVE_AS') dispatch('LISTEN_FOR_MOVE_TO') dispatch('LISTEN_FOR_SAVE') + dispatch('LISTEN_FOR_SAVE_TO_IPFS') dispatch('LISTEN_FOR_SET_PATHNAME') dispatch('LISTEN_FOR_BOOTSTRAP_WINDOW') dispatch('LISTEN_FOR_SAVE_CLOSE') From 285b9abeeaa467bdf823f7d84c58b38cdf3725aa Mon Sep 17 00:00:00 2001 From: "OEM Configuration (temporary user)" Date: Sun, 14 Aug 2022 06:14:12 +0800 Subject: [PATCH 05/14] add ipfs function add ipfs function --- src/main/filesystem/index.js | 18 ++++++++++++++++++ src/main/filesystem/markdown.js | 22 ++++++++++++++++++++++ src/main/menu/actions/file.js | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index 870738af..372afe8f 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -30,3 +30,21 @@ export const writeFile = (pathname, content, extension, options = 'utf-8') => { return fs.outputFile(pathname, content, options) } + +export const writeFileToIpfs = (pathname, content, extension, options = 'utf-8') => { + if (!pathname) { + return Promise.reject(new Error('[ERROR] Cannot save file without path.')) + } + pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` + + try { + const IPFS = await import('ipfs-core') + const node = await IPFS.create(); + const id = await node.id(); + console.log(id); + } catch (err) { + console.error(err); + } + + return fs.outputFile(pathname, content, options) +} diff --git a/src/main/filesystem/markdown.js b/src/main/filesystem/markdown.js index 46a01ce3..f438bafb 100644 --- a/src/main/filesystem/markdown.js +++ b/src/main/filesystem/markdown.js @@ -67,6 +67,28 @@ export const writeMarkdownFile = (pathname, content, options) => { return writeFile(pathname, buffer, extension, undefined) } +/** + * Write the content file to ipfs. + * + * @param {string} pathname The path to the file. + * @param {string} content The buffer to save. + * @param {IMarkdownDocumentOptions} options The markdown document options + */ + export const writeMarkdownFileToIpfs = (pathname, content, options) => { + const { adjustLineEndingOnSave, lineEnding } = options + const { encoding, isBom } = options.encoding + const extension = path.extname(pathname) || '.md' + + if (adjustLineEndingOnSave) { + content = convertLineEndings(content, lineEnding) + } + + const buffer = iconv.encode(content, encoding, { addBOM: isBom }) + + // TODO(@fxha): "safeSaveDocuments" using temporary file and rename syscall. + return writeFileToIpfs(pathname, buffer, extension, undefined) +} + /** * Reads the contents of a markdown file. * diff --git a/src/main/menu/actions/file.js b/src/main/menu/actions/file.js index 01cb8fee..94f22455 100644 --- a/src/main/menu/actions/file.js +++ b/src/main/menu/actions/file.js @@ -190,7 +190,7 @@ const handleResponseForSaveToIpfs = async (e, { id, filename, markdown, pathname filePath = path.resolve(filePath) const extension = path.extname(filePath) || '.md' filePath = !filePath.endsWith(extension) ? filePath += extension : filePath - return writeMarkdownFile(filePath, markdown, options, win) + return writeMarkdownFileToIpfs(filePath, markdown, options, win) .then(() => { if (!alreadyExistOnDisk) { ipcMain.emit('window-add-file-path', win.id, filePath) From 6f00be73ff4e1f60b022a8e126605c70d9742549 Mon Sep 17 00:00:00 2001 From: "OEM Configuration (temporary user)" Date: Sun, 14 Aug 2022 06:28:59 +0800 Subject: [PATCH 06/14] add import for ipfs func --- src/main/filesystem/markdown.js | 2 +- src/main/menu/actions/file.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/filesystem/markdown.js b/src/main/filesystem/markdown.js index f438bafb..486e66b7 100644 --- a/src/main/filesystem/markdown.js +++ b/src/main/filesystem/markdown.js @@ -5,7 +5,7 @@ import iconv from 'iconv-lite' import { LINE_ENDING_REG, LF_LINE_ENDING_REG, CRLF_LINE_ENDING_REG } from '../config' import { isDirectory2 } from 'common/filesystem' import { isMarkdownFile } from 'common/filesystem/paths' -import { normalizeAndResolvePath, writeFile } from '../filesystem' +import { normalizeAndResolvePath, writeFile, writeFileToIpfs } from '../filesystem' import { guessEncoding } from './encoding' const getLineEnding = lineEnding => { diff --git a/src/main/menu/actions/file.js b/src/main/menu/actions/file.js index 94f22455..bf7d2806 100644 --- a/src/main/menu/actions/file.js +++ b/src/main/menu/actions/file.js @@ -9,7 +9,7 @@ import { showTabBar } from './view' import { COMMANDS } from '../../commands' import { EXTENSION_HASN, PANDOC_EXTENSIONS, URL_REG } from '../../config' import { normalizeAndResolvePath, writeFile } from '../../filesystem' -import { writeMarkdownFile } from '../../filesystem/markdown' +import { writeMarkdownFile, writeMarkdownFileToIpfs} from '../../filesystem/markdown' import { getPath, getRecommendTitleFromMarkdownString } from '../../utils' import pandoc from '../../utils/pandoc' From 1294dda3dd445681837dc20a6705dc406ac3f530 Mon Sep 17 00:00:00 2001 From: "OEM Configuration (temporary user)" Date: Sun, 14 Aug 2022 06:47:10 +0800 Subject: [PATCH 07/14] add import for ipfs func --- src/main/filesystem/index.js | 10 +++++----- src/main/menu/actions/file.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index 372afe8f..2462c12f 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -38,12 +38,12 @@ export const writeFileToIpfs = (pathname, content, extension, options = 'utf-8') pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` try { - const IPFS = await import('ipfs-core') - const node = await IPFS.create(); - const id = await node.id(); - console.log(id); +// const IPFS = await import('ipfs-core') +// const node = await IPFS.create(); +// const id = await node.id(); + console.log('id') } catch (err) { - console.error(err); + console.error(err) } return fs.outputFile(pathname, content, options) diff --git a/src/main/menu/actions/file.js b/src/main/menu/actions/file.js index bf7d2806..ea9c31dd 100644 --- a/src/main/menu/actions/file.js +++ b/src/main/menu/actions/file.js @@ -8,8 +8,8 @@ import { checkUpdates, userSetting } from './marktext' import { showTabBar } from './view' import { COMMANDS } from '../../commands' import { EXTENSION_HASN, PANDOC_EXTENSIONS, URL_REG } from '../../config' -import { normalizeAndResolvePath, writeFile } from '../../filesystem' -import { writeMarkdownFile, writeMarkdownFileToIpfs} from '../../filesystem/markdown' +import { normalizeAndResolvePath, writeFile, writeFileToIpfs } from '../../filesystem' +import { writeMarkdownFile, writeMarkdownFileToIpfs } from '../../filesystem/markdown' import { getPath, getRecommendTitleFromMarkdownString } from '../../utils' import pandoc from '../../utils/pandoc' From 9f2d354b171a748ace9f92cc6da490ada1bdd76d Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 17 Aug 2022 09:07:19 +0800 Subject: [PATCH 08/14] add ipfs add file func add ipfs add file func --- src/main/filesystem/index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index 2462c12f..44d617ae 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -1,5 +1,6 @@ import fs from 'fs-extra' import path from 'path' +import * as IPFS from 'ipfs-core' import { isDirectory, isFile, isSymbolicLink } from 'common/filesystem' /** @@ -38,10 +39,17 @@ export const writeFileToIpfs = (pathname, content, extension, options = 'utf-8') pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` try { -// const IPFS = await import('ipfs-core') -// const node = await IPFS.create(); -// const id = await node.id(); - console.log('id') + const node = await IPFS.create() + const version = await node.version() + + console.log('Version:', version.version) + const file = await node.add({ + path: pathname, + content: content + }); + + console.log('Added file:', file.path, file.cid.toString()); + } catch (err) { console.error(err) } From e051e24bc45fafaf8db6bd0384442f6082d444e9 Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 17 Aug 2022 09:19:28 +0800 Subject: [PATCH 09/14] Update index.js Update index.js --- src/main/filesystem/index.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index 44d617ae..bcbce2c5 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -38,21 +38,22 @@ export const writeFileToIpfs = (pathname, content, extension, options = 'utf-8') } pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` - try { - const node = await IPFS.create() - const version = await node.version() - - console.log('Version:', version.version) - const file = await node.add({ - path: pathname, - content: content - }); - - console.log('Added file:', file.path, file.cid.toString()); + async () => { + try { + const node = await IPFS.create() + const version = await node.version() - } catch (err) { - console.error(err) + console.log('Version:', version.version) + const file = await node.add({ + path: pathname, + content: content + }); + + console.log('Added file:', file.path, file.cid.toString()); + + } catch (err) { + console.error(err) + } } - return fs.outputFile(pathname, content, options) } From 7b1a1ecadb7bf31b217614aaec00f4490139f46c Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 17 Aug 2022 09:21:10 +0800 Subject: [PATCH 10/14] Update index.js Update index.js --- src/main/filesystem/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index bcbce2c5..3ab0d3cc 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -38,6 +38,7 @@ export const writeFileToIpfs = (pathname, content, extension, options = 'utf-8') } pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` + // eslint-disable-next-line no-unused-expressions async () => { try { const node = await IPFS.create() From 513e2666dd58ccd2af4001b500eba0b6361aaf29 Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 7 Sep 2022 09:35:40 +0800 Subject: [PATCH 11/14] update writeToIpfs update writeToIpfs --- package.json | 4 +--- src/main/filesystem/index.js | 23 ++++++----------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index bb53052b..ee9045b6 100644 --- a/package.json +++ b/package.json @@ -86,9 +86,7 @@ "vue-router": "^3.5.3", "vuex": "^3.6.2", "webfontloader": "^1.6.28", - "ipfs-core": "^0.15.2", - "it-all": "^1.0.4", - "uint8arrays": "^3.0.0" + "web3.storage": "latest" }, "devDependencies": { "@babel/core": "^7.17.7", diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index 3ab0d3cc..f02d98ac 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -1,6 +1,6 @@ import fs from 'fs-extra' import path from 'path' -import * as IPFS from 'ipfs-core' +import { Web3Storage, File } from 'web3.storage' import { isDirectory, isFile, isSymbolicLink } from 'common/filesystem' /** @@ -38,23 +38,12 @@ export const writeFileToIpfs = (pathname, content, extension, options = 'utf-8') } pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` - // eslint-disable-next-line no-unused-expressions - async () => { - try { - const node = await IPFS.create() - const version = await node.version() + const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDU4ZDc1ZjYzN2Y5NDc2YzVkQmU1OGIxNzEyN0Q1MGU0NDgxMzUzQjQiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjE0MDU2Mzc2MDQsIm5hbWUiOiJ4aW5taW5zdSJ9.sb1ATMTwOtsquSn6kTWQylCRUZjVDWrGUq5o6sLHlis"; + const storage = new Web3Storage({ token }); - console.log('Version:', version.version) - const file = await node.add({ - path: pathname, - content: content - }); + const files = await getFilesFromPath(pathname); + const cid = await storage.put(files); + console.log('Content added with CID:', cid); - console.log('Added file:', file.path, file.cid.toString()); - - } catch (err) { - console.error(err) - } - } return fs.outputFile(pathname, content, options) } From cfd91d8ac49d7bfe319f9fecf975f0ee8c00184a Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 7 Sep 2022 10:59:08 +0800 Subject: [PATCH 12/14] update code format. update code format. --- src/main/filesystem/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index f02d98ac..841e68b9 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -1,6 +1,6 @@ import fs from 'fs-extra' import path from 'path' -import { Web3Storage, File } from 'web3.storage' +import { Web3Storage, getFilesFromPath } from 'web3.storage' import { isDirectory, isFile, isSymbolicLink } from 'common/filesystem' /** @@ -32,18 +32,18 @@ export const writeFile = (pathname, content, extension, options = 'utf-8') => { return fs.outputFile(pathname, content, options) } -export const writeFileToIpfs = (pathname, content, extension, options = 'utf-8') => { +export const writeFileToIpfs = async (pathname, content, extension, options = 'utf-8') => { if (!pathname) { return Promise.reject(new Error('[ERROR] Cannot save file without path.')) } pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` - const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDU4ZDc1ZjYzN2Y5NDc2YzVkQmU1OGIxNzEyN0Q1MGU0NDgxMzUzQjQiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjE0MDU2Mzc2MDQsIm5hbWUiOiJ4aW5taW5zdSJ9.sb1ATMTwOtsquSn6kTWQylCRUZjVDWrGUq5o6sLHlis"; - const storage = new Web3Storage({ token }); + const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDU4ZDc1ZjYzN2Y5NDc2YzVkQmU1OGIxNzEyN0Q1MGU0NDgxMzUzQjQiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjE0MDU2Mzc2MDQsIm5hbWUiOiJ4aW5taW5zdSJ9.sb1ATMTwOtsquSn6kTWQylCRUZjVDWrGUq5o6sLHlis' + const storage = new Web3Storage({ token }) - const files = await getFilesFromPath(pathname); - const cid = await storage.put(files); - console.log('Content added with CID:', cid); + const files = await getFilesFromPath(pathname) + const cid = await storage.put(files) + console.log('Content added with CID:', cid) return fs.outputFile(pathname, content, options) } From f25d58718027072b25d70f1e7341a07600b849e7 Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 7 Sep 2022 14:47:44 +0800 Subject: [PATCH 13/14] move ipfs to ipcMain.on move ipfs to ipcMain.on --- src/main/app/windowManager.js | 11 +++++++++++ src/main/filesystem/index.js | 8 -------- src/main/menu/actions/file.js | 1 + 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/app/windowManager.js b/src/main/app/windowManager.js index 61e6129c..acf5d398 100644 --- a/src/main/app/windowManager.js +++ b/src/main/app/windowManager.js @@ -3,6 +3,7 @@ import EventEmitter from 'events' import log from 'electron-log' import Watcher, { WATCHER_STABILITY_THRESHOLD, WATCHER_STABILITY_POLL_INTERVAL } from '../filesystem/watcher' import { WindowType } from '../windows/base' +import { Web3Storage, getFilesFromPath } from 'web3.storage' class WindowActivityList { constructor () { @@ -424,6 +425,16 @@ class WindowManager extends EventEmitter { this._watcher.ignoreChangedEvent(windowId, pathname, duration) }) + ipcMain.on('add-file-to-ipfs', async (pathname) => { + // A changed event is emitted earliest after the stability threshold. + const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDU4ZDc1ZjYzN2Y5NDc2YzVkQmU1OGIxNzEyN0Q1MGU0NDgxMzUzQjQiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjE0MDU2Mzc2MDQsIm5hbWUiOiJ4aW5taW5zdSJ9.sb1ATMTwOtsquSn6kTWQylCRUZjVDWrGUq5o6sLHlis' + const storage = new Web3Storage({ token }) + + const files = await getFilesFromPath(pathname) + const cid = await storage.put(files) + console.log('Content added with CID:', cid) + }) + ipcMain.on('window-close-by-id', id => { this.forceCloseById(id) }) diff --git a/src/main/filesystem/index.js b/src/main/filesystem/index.js index 841e68b9..22d77dde 100644 --- a/src/main/filesystem/index.js +++ b/src/main/filesystem/index.js @@ -1,6 +1,5 @@ import fs from 'fs-extra' import path from 'path' -import { Web3Storage, getFilesFromPath } from 'web3.storage' import { isDirectory, isFile, isSymbolicLink } from 'common/filesystem' /** @@ -38,12 +37,5 @@ export const writeFileToIpfs = async (pathname, content, extension, options = 'u } pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}` - const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDU4ZDc1ZjYzN2Y5NDc2YzVkQmU1OGIxNzEyN0Q1MGU0NDgxMzUzQjQiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjE0MDU2Mzc2MDQsIm5hbWUiOiJ4aW5taW5zdSJ9.sb1ATMTwOtsquSn6kTWQylCRUZjVDWrGUq5o6sLHlis' - const storage = new Web3Storage({ token }) - - const files = await getFilesFromPath(pathname) - const cid = await storage.put(files) - console.log('Content added with CID:', cid) - return fs.outputFile(pathname, content, options) } diff --git a/src/main/menu/actions/file.js b/src/main/menu/actions/file.js index ea9c31dd..18742255 100644 --- a/src/main/menu/actions/file.js +++ b/src/main/menu/actions/file.js @@ -202,6 +202,7 @@ const handleResponseForSaveToIpfs = async (e, { id, filename, markdown, pathname ipcMain.emit('window-file-saved', win.id, filePath) win.webContents.send('mt::tab-saved', id) } + ipcMain.emit('add-file-to-ipfs', filePath) return id }) .catch(err => { From 45e8591936ee4142ae6fc51f61b83bf6c01c7986 Mon Sep 17 00:00:00 2001 From: xinminsu Date: Wed, 7 Sep 2022 15:41:04 +0800 Subject: [PATCH 14/14] remove unused function remove unused function --- src/main/filesystem/markdown.js | 2 +- src/main/menu/actions/file.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/filesystem/markdown.js b/src/main/filesystem/markdown.js index 486e66b7..9d5ada10 100644 --- a/src/main/filesystem/markdown.js +++ b/src/main/filesystem/markdown.js @@ -74,7 +74,7 @@ export const writeMarkdownFile = (pathname, content, options) => { * @param {string} content The buffer to save. * @param {IMarkdownDocumentOptions} options The markdown document options */ - export const writeMarkdownFileToIpfs = (pathname, content, options) => { +export const writeMarkdownFileToIpfs = (pathname, content, options) => { const { adjustLineEndingOnSave, lineEnding } = options const { encoding, isBom } = options.encoding const extension = path.extname(pathname) || '.md' diff --git a/src/main/menu/actions/file.js b/src/main/menu/actions/file.js index 18742255..f9c0b9eb 100644 --- a/src/main/menu/actions/file.js +++ b/src/main/menu/actions/file.js @@ -8,7 +8,7 @@ import { checkUpdates, userSetting } from './marktext' import { showTabBar } from './view' import { COMMANDS } from '../../commands' import { EXTENSION_HASN, PANDOC_EXTENSIONS, URL_REG } from '../../config' -import { normalizeAndResolvePath, writeFile, writeFileToIpfs } from '../../filesystem' +import { normalizeAndResolvePath, writeFile } from '../../filesystem' import { writeMarkdownFile, writeMarkdownFileToIpfs } from '../../filesystem/markdown' import { getPath, getRecommendTitleFromMarkdownString } from '../../utils' import pandoc from '../../utils/pandoc'