This repository was archived by the owner on May 24, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathcode.js
More file actions
116 lines (94 loc) · 2.93 KB
/
Copy pathcode.js
File metadata and controls
116 lines (94 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React from 'react'
import {
EditorBlock,
EditorState,
RichUtils
} from 'draft-js'
import axios from "axios"
import { updateDataOfBlock, addNewBlockAt } from '../../model/index.js'
import {image} from "../icons.js"
import Select from 'react-select'
export default class CodeBlock extends React.Component {
constructor(props) {
super(props)
this.state = {
syntax: this.props.blockProps.data.get('syntax')
}
this.languages = this.props.blockProps.config.languages
}
componentDidMount() {
//this.updateData({syntax: "javascript"})
}
// will update block state
updateData = (options)=> {
let { blockProps, block } = this.props
let { getEditorState } = blockProps
let { setEditorState } = blockProps
let data = block.getData()
let newData = data.merge(this.state).merge(options)
return setEditorState(updateDataOfBlock(getEditorState(), block, newData))
}
renderSelect = ()=>{
return this.props.blockProps.config.displaySelect && !this.props.blockProps.getEditor().props.read_only
}
render = ()=> {
return (
<div>
<span className="dante-code-syntax">
{/*this.props.blockProps.data.get('syntax')*/}
{
this.renderSelect() ?
<Select options={this.languages}
isSearchable={true}
defaultValue={this.state.syntax}
onChange={(o)=>{
this.updateData({syntax: o.value})
}} /> : null
}
</span>
<EditorBlock {...this.props}/>
</div>
)
}
}
export const CodeBlockConfig = (options={})=>{
let config = {
title: 'add an code',
type: 'code-block',
icon: image,
block: CodeBlock,
editable: true,
renderable: true,
//breakOnContinuous: true,
wrapper_class: "graf graf--code",
selected_class: "is-selected",
selectedFn: block => {},
handleEnterWithoutText(ctx, block) {
const { editorState } = ctx.state
return ctx.onChange(addNewBlockAt(editorState, block.getKey()))
},
handleEnterWithText(ctx, block) {
const { editorState } = ctx.state
const selection = editorState.getSelection()
// check if we are in the last line and got 2 previous breaklines
if(block.getLength() === selection.getEndOffset()){
if(block.getText().slice(-2) === "\n\n"){
return ctx.onChange(addNewBlockAt(editorState, block.getKey()))
}
}
return ctx.onChange(RichUtils.insertSoftNewline(editorState))
},
widget_options: {
},
options: {
displaySelect: true,
languages: [
{ value: 'javascript', label: 'javascript' },
{ value: 'html', label: 'html' },
{ value: 'css', label: 'css' },
{ value: null, label: 'none'}
]
}
}
return Object.assign(config, options)
}