feat: suggest TagName on create

This commit is contained in:
tiddlygit-test 2020-08-22 16:49:35 +08:00
parent 6d6bbee015
commit 5d932c7fb8
6 changed files with 75 additions and 16 deletions

View file

@ -1,9 +1,17 @@
const path = require('path');
const fs = require('fs-extra');
const { take, drop, compact } = require('lodash');
const { logger } = require('../log');
const getMatchPart = tagToMatch => `[!is[system]kin::to[${tagToMatch}]`;
const getPathPart = folderToPlace => `addprefix[subwiki/${folderToPlace}/]]`;
const getTagNameFromMatchPart = matchPart => matchPart.replace('[!is[system]kin::to[', '').replace(/].*/, '');
const getFolderNamePathPart = pathPart => pathPart.replace(/.+addprefix\[subwiki\//, '').replace('/]]', '');
function getFileSystemPathsTiddlerPath(mainWikiPath) {
const pluginPath = path.join(mainWikiPath, 'plugins', 'linonetwo', 'sub-wiki');
return path.join(pluginPath, 'FileSystemPaths.tid');
}
/**
* update $:/config/FileSystemPaths programmatically to make private tiddlers goto the sub-wiki
@ -12,8 +20,7 @@ const getPathPart = folderToPlace => `addprefix[subwiki/${folderToPlace}/]]`;
* @param {Object} oldConfig if you need to replace a line, you need to pass-in what old line looks like, so here we can find and replace it
*/
function updateSubWikiPluginContent(mainWikiPath, newConfig, oldConfig) {
const pluginPath = path.join(mainWikiPath, 'plugins', 'linonetwo', 'sub-wiki');
const FileSystemPathsTiddlerPath = path.join(pluginPath, 'FileSystemPaths.tid');
const FileSystemPathsTiddlerPath = getFileSystemPathsTiddlerPath(mainWikiPath);
const FileSystemPathsFile = fs.readFileSync(FileSystemPathsTiddlerPath, 'utf-8');
let newFileSystemPathsFile = '';
@ -66,6 +73,23 @@ function updateSubWikiPluginContent(mainWikiPath, newConfig, oldConfig) {
fs.writeFileSync(FileSystemPathsTiddlerPath, newFileSystemPathsFile);
}
async function getSubWikiPluginContent(mainWikiPath) {
if (!mainWikiPath) return [];
const FileSystemPathsTiddlerPath = getFileSystemPathsTiddlerPath(mainWikiPath);
try {
const FileSystemPathsFile = await fs.readFile(FileSystemPathsTiddlerPath, 'utf-8');
const FileSystemPaths = compact(drop(FileSystemPathsFile.split('\n'), 3));
return FileSystemPaths.map(line => ({
tagName: getTagNameFromMatchPart(line),
folderName: getFolderNamePathPart(line),
}));
} catch (error) {
logger.error(error, { function: 'getSubWikiPluginContent' });
return [];
}
}
module.exports = {
updateSubWikiPluginContent,
getSubWikiPluginContent,
};

View file

@ -5,6 +5,7 @@ const { autoUpdater } = require('electron-updater');
const { initWikiGit, getRemoteUrl } = require('../libs/git');
const { stopWatchWiki, watchWiki } = require('../libs/wiki/watch-wiki');
const { getSubWikiPluginContent } = require('../libs/wiki/update-plugin-content');
const { stopWiki, startWiki } = require('../libs/wiki/wiki-worker-mamager');
const { logger } = require('../libs/log');
const {
@ -123,6 +124,7 @@ const loadListeners = () => {
return String(error);
}
});
ipcMain.handle('get-sub-wiki-plugin-content', (event, mainWikiPath) => getSubWikiPluginContent(mainWikiPath));
ipcMain.on('get-constant', (event, name) => {
event.returnValue = {
ICON_PATH,

View file

@ -15,6 +15,7 @@ import FormHelperText from '@material-ui/core/FormHelperText';
import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
import FolderIcon from '@material-ui/icons/Folder';
import Autocomplete from '@material-ui/lab/Autocomplete';
import * as actions from '../../state/dialog-add-workspace/actions';
@ -51,6 +52,7 @@ type OwnProps = {|
existedFolderLocation: string,
wikiPort: number,
wikiPortSetter: number => void,
fileSystemPaths: { tagName: string, folderName: string }[],
isCreateMainWorkspace: boolean,
|};
type DispatchProps = {|
@ -79,6 +81,7 @@ function WikiPathForm({
mainWikiToLinkSetter,
wikiPort,
wikiPortSetter,
fileSystemPaths,
isCreateMainWorkspace,
}: Props) {
const [workspaces, workspacesSetter] = useState({});
@ -139,13 +142,6 @@ function WikiPathForm({
)}
{!isCreateMainWorkspace && (
<>
<TextField
fullWidth
onChange={event => tagNameSetter(event.target.value)}
label={t('AddWorkspace.TagName')}
helperText={t('AddWorkspace.TagNameHelp')}
value={tagName}
/>
<SoftLinkToMainWikiSelectInputLabel id="main-wiki-select-label">
{t('AddWorkspace.MainWorkspaceLocation')}
</SoftLinkToMainWikiSelectInputLabel>
@ -178,6 +174,20 @@ function WikiPathForm({
</Typography>
</FormHelperText>
)}
<Autocomplete
freeSolo
options={fileSystemPaths.map(fileSystemPath => fileSystemPath.tagName)}
renderInput={parameters => (
<TextField
{...parameters}
onChange={event => tagNameSetter(event.target.value)}
value={tagName}
fullWidth
label={t('AddWorkspace.TagName')}
helperText={t('AddWorkspace.TagNameHelp')}
/>
)}
/>
</>
)}
</CreateContainer>

View file

@ -27,6 +27,7 @@ import {
countWorkspace,
getWorkspaceRemote,
requestOpen,
getSubWikiPluginContent,
} from '../../senders';
const graphqlClient = new GraphQLClient({
@ -84,6 +85,13 @@ export default function AddWorkspace() {
}, [userInfo]);
const [mainWikiToLink, mainWikiToLinkSetter] = useState({ name: '', port: 0 });
const [fileSystemPaths, fileSystemPathsSetter] = useState([]);
useEffect(() => {
// eslint-disable-next-line promise/catch-or-return
getSubWikiPluginContent(mainWikiToLink.name).then(fileSystemPathsSetter);
}, [mainWikiToLink]);
// DEBUG: console
console.log(`fileSystemPaths`, fileSystemPaths);
const [githubWikiUrl, githubWikiUrlSetter] = useState<string>('');
const [tagName, tagNameSetter] = useState<string>('');
useEffect(() => {
@ -168,6 +176,7 @@ export default function AddWorkspace() {
mainWikiToLinkSetter={mainWikiToLinkSetter}
wikiPort={wikiPort}
wikiPortSetter={wikiPortSetter}
fileSystemPaths={fileSystemPaths}
isCreateMainWorkspace={isCreateMainWorkspace}
/>
@ -196,6 +205,7 @@ export default function AddWorkspace() {
mainWikiToLinkSetter={mainWikiToLinkSetter}
wikiPort={wikiPort}
wikiPortSetter={wikiPortSetter}
fileSystemPaths={fileSystemPaths}
isCreateMainWorkspace={isCreateMainWorkspace}
/>
@ -223,6 +233,7 @@ export default function AddWorkspace() {
mainWikiToLinkSetter={mainWikiToLinkSetter}
wikiPort={wikiPort}
wikiPortSetter={wikiPortSetter}
fileSystemPaths={fileSystemPaths}
isCreateMainWorkspace={isCreateMainWorkspace}
/>
<CloneWikiDoneButton

View file

@ -15,6 +15,7 @@ import FormHelperText from '@material-ui/core/FormHelperText';
import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
import FolderIcon from '@material-ui/icons/Folder';
import Autocomplete from '@material-ui/lab/Autocomplete';
import * as actions from '../../state/dialog-add-workspace/actions';
@ -50,6 +51,7 @@ type OwnProps = {|
parentFolderLocation: string,
wikiPort: number,
wikiPortSetter: number => void,
fileSystemPaths: { tagName: string, folderName: string }[],
isCreateMainWorkspace: boolean,
|};
type DispatchProps = {|
@ -73,6 +75,7 @@ function NewWikiPathForm({
mainWikiToLinkSetter,
wikiPort,
wikiPortSetter,
fileSystemPaths,
isCreateMainWorkspace,
}: Props) {
const [workspaces, workspacesSetter] = useState({});
@ -143,13 +146,6 @@ function NewWikiPathForm({
)}
{!isCreateMainWorkspace && (
<>
<TextField
fullWidth
onChange={event => tagNameSetter(event.target.value)}
label={t('AddWorkspace.TagName')}
helperText={t('AddWorkspace.TagNameHelp')}
value={tagName}
/>
<SoftLinkToMainWikiSelectInputLabel id="main-wiki-select-label">
{t('AddWorkspace.MainWorkspaceLocation')}
</SoftLinkToMainWikiSelectInputLabel>
@ -182,6 +178,20 @@ function NewWikiPathForm({
</Typography>
</FormHelperText>
)}
<Autocomplete
freeSolo
options={fileSystemPaths.map(fileSystemPath => fileSystemPath.tagName)}
renderInput={parameters => (
<TextField
{...parameters}
onChange={event => tagNameSetter(event.target.value)}
value={tagName}
fullWidth
label={t('AddWorkspace.TagName')}
helperText={t('AddWorkspace.TagNameHelp')}
/>
)}
/>
</>
)}
</CreateContainer>

View file

@ -37,6 +37,8 @@ export const requestCloneSubWiki = (
userInfo,
tagName,
);
export const getSubWikiPluginContent = (mainWikiPath: string): Promise<{ tagName: string, folderName: string }[]> =>
ipcRenderer.invoke('get-sub-wiki-plugin-content', mainWikiPath);
export const requestOpen = (uri: string, isDirectory?: boolean) => ipcRenderer.send('request-open', uri, !!isDirectory);
export const requestShowMessageBox = (message: string, type: string) =>
ipcRenderer.send('request-show-message-box', message, type);