Skip to content
Snippets Groups Projects
Commit 5ca1a596 authored by chris's avatar chris
Browse files

add new tag to the list

parent 24e446ca
No related branches found
No related tags found
1 merge request!232feat(custom-tag-block): added custom-tag-block
import React, { useContext, useMemo, useRef, useState } from 'react'; import React, { useContext, useMemo, useRef, useState, useEffect } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { WaxContext } from 'wax-prosemirror-core'; import { WaxContext } from 'wax-prosemirror-core';
import useDeepCompareEffect from 'use-deep-compare-effect'; import useDeepCompareEffect from 'use-deep-compare-effect';
...@@ -11,6 +11,7 @@ const Input = styled.input` ...@@ -11,6 +11,7 @@ const Input = styled.input`
border-bottom: 1px solid grey; border-bottom: 1px solid grey;
font-size: 14px; font-size: 14px;
font-weight: 400; font-weight: 400;
width: 150px;
&:focus { &:focus {
outline: none; outline: none;
...@@ -19,6 +20,7 @@ const Input = styled.input` ...@@ -19,6 +20,7 @@ const Input = styled.input`
const FlexDiv = styled.div` const FlexDiv = styled.div`
display: flex; display: flex;
width: 150px;
`; `;
const Add = styled.button` const Add = styled.button`
...@@ -48,12 +50,10 @@ const Box = styled.div` ...@@ -48,12 +50,10 @@ const Box = styled.div`
const StyledButton = styled.div``; const StyledButton = styled.div``;
const CustomTagBlockComponent = props => { const CustomTagBlockComponent = ({ isShowTag, item }) => {
const { isShowTag, item } = props;
const ref = useRef(); const ref = useRef();
const [inputValue, setInputValue] = useState(''); const [inputValue, setInputValue] = useState('');
const [tagName, setTagName] = useState(''); const [tagName, setTagName] = useState('');
const localTagList = JSON.parse(localStorage.getItem('tagBlockList'));
const { const {
app, app,
...@@ -66,27 +66,20 @@ const CustomTagBlockComponent = props => { ...@@ -66,27 +66,20 @@ const CustomTagBlockComponent = props => {
const { $from } = state.selection; const { $from } = state.selection;
const type = $from.parent.attrs.class ? $from.parent.attrs.class : ''; const type = $from.parent.attrs.class ? $from.parent.attrs.class : '';
const serviceConfig = app.config.get('config.CustomTagService'); const configTags = app.config.get('config.CustomTagService').tags;
const [serviceList, setServiceList] = useState([]); const [allTags, setAllTags] = useState(configTags);
const isActive = item.active(activeView.state, activeViewId, type); const isActive = item.active(activeView.state, activeViewId, type);
const onChangeTagName = e => { const onChangeTagName = () => {
setTagName(e.target.value); setTagName(ref.current.value);
setInputValue(e.target.value); setInputValue(ref.current.value);
}; };
const onClickAdd = () => { const onClickAdd = () => {
if (tagName.trim() === '') return; if (tagName.trim() === '') return;
let tagNameList = [];
if (localStorage.getItem('tagBlockList') === null) { configTags.push({ label: tagName, tagType: 'block' });
tagNameList.push({ label: tagName, type: 'block' }); setAllTags(configTags);
localStorage.setItem('tagBlockList', JSON.stringify(tagNameList));
} else {
tagNameList = JSON.parse(localStorage.getItem('tagBlockList'));
tagNameList.push({ label: tagName, type: 'block' });
localStorage.clear('tagBlockList');
localStorage.setItem('tagBlockList', JSON.stringify(tagNameList));
}
setInputValue(' '); setInputValue(' ');
}; };
...@@ -94,22 +87,7 @@ const CustomTagBlockComponent = props => { ...@@ -94,22 +87,7 @@ const CustomTagBlockComponent = props => {
item.run(state, dispatch, val.replace(/ /g, '-')); item.run(state, dispatch, val.replace(/ /g, '-'));
}; };
useDeepCompareEffect(() => { console.log(allTags);
const labels = [];
if (serviceConfig !== undefined) {
serviceConfig.tags.forEach(tag => {
if (tag.tagType === 'block') {
labels.push(tag.label);
}
});
}
if (localTagList !== null) {
localTagList.forEach(tag => {
labels.push(tag.label);
});
}
setServiceList(labels);
}, [localTagList, serviceConfig]);
return useMemo( return useMemo(
() => ( () => (
...@@ -117,7 +95,7 @@ const CustomTagBlockComponent = props => { ...@@ -117,7 +95,7 @@ const CustomTagBlockComponent = props => {
{isShowTag && ( {isShowTag && (
<FlexDiv> <FlexDiv>
<Input <Input
onChange={e => onChangeTagName(e)} onChange={onChangeTagName}
ref={ref} ref={ref}
type="text" type="text"
value={inputValue} value={inputValue}
...@@ -127,7 +105,7 @@ const CustomTagBlockComponent = props => { ...@@ -127,7 +105,7 @@ const CustomTagBlockComponent = props => {
)} )}
</Wrapper> </Wrapper>
), ),
[], [isShowTag, inputValue],
); );
}; };
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment