Skip to content

Commit

Permalink
feat: 增加导出全部代码功能
Browse files Browse the repository at this point in the history
  • Loading branch information
happice0746 committed Sep 22, 2024
1 parent b4eff3f commit 67222eb
Show file tree
Hide file tree
Showing 26 changed files with 352 additions and 39 deletions.
30 changes: 30 additions & 0 deletions scripts/codeContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export class CodeContext {
private code: string
private indentLevel: number
constructor () {
this.code = ''
this.indentLevel = 0
}

pushCode (code: string) {
this.code += code
}

newLine () {
this.pushCode('\n' + ' '.repeat(this.indentLevel))
}

indent () {
++this.indentLevel
this.newLine()
}

deindent () {
--this.indentLevel
this.newLine()
}

getCode () {
return this.code
}
}
35 changes: 35 additions & 0 deletions scripts/exportAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Snippet } from '../src/shared/types/main/db'
import { CodeContext } from './codeContext'
import { existsSync, mkdir, writeFile } from 'fs'
import path from 'path'
import { FILES_SUFFIX } from '../src/main/constants'
export const exportAllSnippets = (snippets: Snippet[], targetPath: string) => {
if (snippets.length === 0) {
return
}
const cwd = path.join(targetPath, 'snippets')
for (let i = 0; i < snippets.length; i++) {
const snippetContents = snippets[i].content
const suffix = FILES_SUFFIX[snippets[i].content[0].language]
const code = new CodeContext()
for (let j = 0; j < snippetContents.length; j++) {
code.pushCode(snippetContents[j].value)
code.newLine()
}
if (existsSync(cwd)) {
const filePath = path.join(cwd, `${snippets[i].name}${suffix}`)
exportFile(filePath, code.getCode())
} else {
mkdir(cwd, () => {
const filePath = path.join(cwd, `${snippets[i].name}${suffix}`)
exportFile(filePath, code.getCode())
})
}
}
}

const exportFile = (path: string, code: string) => {
writeFile(path, code, () => {
console.log('finish', path)
})
}
189 changes: 189 additions & 0 deletions src/main/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
export const FILES_SUFFIX = {
abap: '.abap',
abc: '.xyz',
actionscript: '.as',
ada: '.ada',
alda: '.alda',
apache_conf: '.conf',
apex: '.cls',
applescript: '.applescript',
aql: '.aql',
asciidoc: '.asciidoc',
asl: '.asl',
asp_vb_net: '.asp',
assembly_x86: '.asm',
autohotkey: '.ahk',
batchfile: '.bat',
bicep: '.bicep',
c_cpp: '.c',
c9search: '.c9search_results',
cirru: '.cirru',
clojure: '.clj',
cobol: '.cob',
coffee: '.coffee',
coldfusion: '.cfml',
crystal: '.cr',
csharp: '.cs',
csound_document: '.csd',
csound_orchestra: '.orc',
csound_score: '.sco',
csp: '.csp',
css: '.css',
curly: '.curly',
d: '.d',
dart: '.dart',
diff: '.diff',
django: '.html',
dockerfile: '.dockerfile',
dot: '.dot',
drools: '.drl',
edifact: '.edi',
eiffel: '.e',
ejs: '.ejs',
elixir: '.ex',
elm: '.elm',
erlang: '.erl',
forth: '.forth',
fortran: '.f90',
fsharp: '.fs',
fsl: '.fsl',
ftl: '.ftl',
gcode: '.gcode',
gherkin: '.feature',
gitignore: '.gitignore',
glsl: '.glsl',
gobstones: '.gbst',
golang: '.go',
graphqlschema: '.graphql',
groovy: '.groovy',
haml: '.haml',
handlebars: '.hbs',
haskell_cabal: '.cabal',
haskell: '.hs',
haxe: '.hx',
hjson: '.hjson',
html_elixir: '.html.eex',
html_ruby: '.html.erb',
html: '.html',
ini: '.ini',
io: '.io',
jack: '.jack',
jade: '.jade',
java: '.java',
javascript: '.js',
json: '.json',
json5: '.json5',
jsoniq: '.jq',
jsp: '.jsp',
jssm: '.jssm',
jsx: '.jsx',
julia: '.jl',
kotlin: '.kt',
kusto: '.kusto',
latex: '.tex',
latte: '.latte',
less: '.less',
liquid: '.liquid',
lisp: '.lisp',
livescript: '.ls',
logiql: '.logic',
logtalk: '.lgt',
lsl: '.lsl',
lua: '.lua',
luapage: '.lp',
lucene: '.lucene',
makefile: '.mk',
markdown: '.md',
mask: '.mask',
matlab: '.matlab',
maze: '.maze',
mediawiki: '.mediawiki',
mel: '.mel',
mikrotik: '.mikrotik',
mips: '.mips',
mixal: '.mixal',
mushcode: '.mush',
mysql: '.sql',
nginx: '.nginx',
nim: '.nim',
nix: '.nix',
nsis: '.nsh',
nunjucks: '.njk',
objectivec: '.m',
ocaml: '.ml',
pascal: '.pas',
perl: '.pl',
perl6: '.p6',
pgsql: '.sql',
php_laravel_blade: '.blade.php',
php: '.php',
pig: '.pig',
plain_text: '.txt',
powershell: '.ps1',
praat: '.praat',
prisma: '.prisma',
prolog: '.prolog',
properties: '.properties',
protobuf: '.proto',
pug: '.pug',
puppet: '.puppet',
python: '.py',
qml: '.qml',
r: '.r',
raku: '.raku',
razor: '.cshtml',
rdoc: '.rdoc',
red: '.red',
redshift: '.sql',
regexp: '.regex',
rhtml: '.rhtml',
rst: '.rst',
ruby: '.rb',
rust: '.rs',
sas: '.sas',
sass: '.scss',
sassdoc: '.scss',
scad: '.scad',
scala: '.scala',
scheme: '.scm',
scrypt: '.scrypt',
scss: '.scss',
sh: '.sh',
sjs: '.sjs',
slim: '.slim',
smalltalk: '.st',
smarty: '.smarty.tpl',
smithy: '.smithy',
solidity: '.sol',
soy_template: '.soy',
space: '.space',
sparql: '.sparql',
sql: '.sql',
sqlserver: '.sql',
stylus: '.styl',
svg: '.svg',
swift: '.swift',
tcl: '.tcl',
terraform: '.tf',
tex: '.tex',
text: '.txt',
textile: '.textile',
toml: '.toml',
tsx: '.tsx',
turtle: '.ttl',
twig: '.twig',
typescript: '.ts',
vala: '.vala',
vbscript: '.vbscript',
velocity: '.vm',
verilog: '.v',
vhdl: '.vhdl',
visualforce: '.visualforce',
vue: '.vue',
wollok: '.wollok',
xml: '.xml',
xquery: '.xquery',
xsl: '.xsl',
yaml: '.yaml',
zeek: '.zeek'
}
20 changes: 19 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, BrowserWindow, ipcMain, Menu, shell } from 'electron'
import { app, BrowserWindow, dialog, ipcMain, Menu, shell } from 'electron'
import path from 'path'
import os from 'os'
import { store } from './store'
Expand All @@ -9,6 +9,7 @@ import { subscribeToChannels } from './services/ipc'
import { mainMenu } from './menu/main'
import { subscribeToDialog } from './services/ipc/dialog'
import { checkForUpdateWithInterval } from './services/update-check'
import { exportAllSnippets } from '../../scripts/exportAll'

const isDev = process.env.NODE_ENV === 'development'
const isMac = process.platform === 'darwin'
Expand Down Expand Up @@ -121,6 +122,23 @@ ipcMain.handle('main:open-url', (event, payload) => {
shell.openExternal(payload as string)
})

ipcMain.handle('main:exportAll', (event, payload: any) => {
const snippets = JSON.parse(payload)
dialog
.showOpenDialog(mainWindow, {
properties: ['openDirectory']
})
.then(result => {
if (!result.canceled) {
console.log(result.filePaths)
exportAllSnippets(snippets, result.filePaths[0])
}
})
.catch(err => {
console.log(err)
})
})

ipcMain.on('request-info', event => {
event.sender.send('request-info', {
version: app.getVersion(),
Expand Down
10 changes: 10 additions & 0 deletions src/main/menu/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ const fileMenu: MenuItemConstructorOptions[] = [
click: () => {
BrowserWindow.getFocusedWindow()?.webContents.send('main-menu:search')
}
},
{
type: 'separator'
},
{
label: i18n.t('exportAll'),
accelerator: 'CommandOrControl+E',
click: () => {
BrowserWindow.getFocusedWindow()?.webContents.send('main-menu:exportAll')
}
}
]

Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/cs/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"newFolder": "Nová složka",
"newSnippet": "Nový snippet",
"exportAll": "Exportovat všechny bloky kódu",
"newFragment": "Nový fragment",
"addDescription": "Přidat popis",
"addToFavorites": "Přidat do oblíbených",
Expand Down
6 changes: 2 additions & 4 deletions src/main/services/i18n/locales/de_DE/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
"fromSnippetsLab": "Von SnippetsLab",
"confirm": "Bestätigen",
"cancel": "Abbrechen",
"update": [
"Nach GitHub",
"OK"
],
"update": ["Nach GitHub", "OK"],
"ok": "OK"
},
"newFolder": "Neuer Ordner",
"newSnippet": "Neues Snippet",
"newFragment": "Neues Fragment",
"addDescription": "Beschreibung hinzufügen",
"exportAll": "Alle Codeblöcke exportieren",
"addToFavorites": "Zu Favoriten hinzufügen",
"addTag": "Tag hinzufügen",
"exportToHtml": "Export zu HTML",
Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/el/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"newSnippet": "Νέο Απόσπασμα",
"newFragment": "Νέο Τμήμα",
"addDescription": "Προσθήκη Περιγραφής",
"exportAll": "Εξαγωγή όλων των κωδικών μπλοκ",
"addToFavorites": "Προσθήκη στα Αγαπημένα",
"addTag": "Προσθήκη Ετικέτας",
"exportToHtml": "Εξαγωγή σε HTML",
Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"newSnippet": "New Snippet",
"newFragment": "New Fragment",
"addDescription": "Add Description",
"exportAll": "Export All Snippets",
"addToFavorites": "Add to Favorites",
"addTag": "Add Tag",
"exportToHtml": "Export to HTML",
Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/es_ES/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"newSnippet": "Nuevo Snippet",
"newFragment": "Nuevo Fragmento",
"addDescription": "Agregar Descripción",
"exportAll": "Exportar todos los bloques de código",
"addToFavorites": "Agregar a Favoritos",
"addTag": "Agregar Etiqueta",
"saveScreenshot": "Guardar Captura",
Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/fa_IR/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"newSnippet": "اسنیپت جدید",
"newFragment": "فرگمنت جدید",
"addDescription": "افزودن توضیحات",
"exportAll": "بلاک های کد را برون برده کن",
"addToFavorites": "افزودن به علاقه‌مندی‌ها",
"addTag": "افزودن برچسب",
"exportToHtml": "استخراج به HTML",
Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/fr_FR/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"newFolder": "Nouveau dossier",
"newSnippet": "Nouvel extrait",
"newFragment": "Nouveau Fragment",
"exportAll": "Exporter tous les blocs de code",
"addDescription": "Ajouter description",
"addToFavorites": "Ajouter aux favoris",
"addTag": "Ajouter une étiquette",
Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/ja_JP/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"newSnippet": "新規スニペット",
"newFragment": "新規フラグメント",
"addDescription": "説明を追加",
"exportAll": "すべてのコードブロックをエクスポート",
"addToFavorites": "お気に入りに追加",
"addTag": "タグを追加",
"exportToHtml": "HTMLにエキスポート",
Expand Down
1 change: 1 addition & 0 deletions src/main/services/i18n/locales/pl/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"newFolder": "Nowy folder",
"newSnippet": "Nowy fragment kodu",
"newFragment": "Nowy fragment",
"exportAll": "Wyeksportuj wszystkie bloki kodu",
"addDescription": "Dodaj opis",
"addToFavorites": "Dodaj do ulubionych",
"addTag": "Dodaj Tag",
Expand Down
Loading

0 comments on commit 67222eb

Please sign in to comment.