rn-rich-text-editor 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -98,25 +98,29 @@ exports.RichEditor = (0, react_1.forwardRef)((props, ref) => {
98
98
  // Non-JSON message, ignore
99
99
  }
100
100
  };
101
+ const sendToWebView = (type) => {
102
+ var _a;
103
+ const data = JSON.stringify({ type });
104
+ const script = `window.dispatchEvent(new MessageEvent('message', { data: ${JSON.stringify(data)} })); true;`;
105
+ (_a = webRef.current) === null || _a === void 0 ? void 0 : _a.injectJavaScript(script);
106
+ };
101
107
  (0, react_1.useImperativeHandle)(ref, () => ({
102
108
  sendAction(type) {
103
- var _a;
104
- (_a = webRef.current) === null || _a === void 0 ? void 0 : _a.postMessage(JSON.stringify({ type }));
109
+ sendToWebView(type);
105
110
  },
106
111
  registerToolbar(listener) {
107
112
  selectionChangeListeners.current.push(listener);
108
113
  },
109
114
  focusContentEditor() {
110
- var _a, _b, _c, _d;
115
+ var _a, _b, _c;
111
116
  if (react_native_1.Platform.OS === 'android') {
112
117
  !keyboardOpen && ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus());
113
118
  (_c = (_b = webRef.current) === null || _b === void 0 ? void 0 : _b.requestFocus) === null || _c === void 0 ? void 0 : _c.call(_b);
114
119
  }
115
- (_d = webRef.current) === null || _d === void 0 ? void 0 : _d.postMessage(JSON.stringify({ type: 'focus' }));
120
+ sendToWebView('focus');
116
121
  },
117
122
  dismissKeyboard() {
118
- var _a;
119
- (_a = webRef.current) === null || _a === void 0 ? void 0 : _a.postMessage(JSON.stringify({ type: 'blur' }));
123
+ sendToWebView('blur');
120
124
  react_native_1.Keyboard.dismiss();
121
125
  },
122
126
  get isKeyboardOpen() {
@@ -131,10 +135,14 @@ exports.RichEditor = (0, react_1.forwardRef)((props, ref) => {
131
135
  },
132
136
  }));
133
137
  return (react_1.default.createElement(react_1.default.Fragment, null,
134
- react_1.default.createElement(react_native_webview_1.WebView, { ref: webRef, originWhitelist: ['*'], javaScriptEnabled: true, scrollEnabled: false, source: (0, createHTML_1.createHTML)(), onMessage: handleMessage, ...props.webViewProps }),
138
+ react_1.default.createElement(react_native_webview_1.WebView, { ref: webRef, originWhitelist: ['*'], javaScriptEnabled: true, scrollEnabled: false, source: (0, createHTML_1.createHTML)(), onMessage: handleMessage, style: [styles.editorWebView, props.style], ...props.webViewProps }),
135
139
  react_native_1.Platform.OS === 'android' && (react_1.default.createElement(react_native_1.TextInput, { ref: inputRef, style: styles.hiddenInput }))));
136
140
  });
137
141
  const styles = react_native_1.StyleSheet.create({
142
+ editorWebView: {
143
+ minHeight: 150,
144
+ backgroundColor: '#fff',
145
+ },
138
146
  hiddenInput: {
139
147
  position: 'absolute',
140
148
  width: 1,
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- export declare const defaultActions: ("bold" | "italic" | "underline" | "checkboxList" | "link" | "keyboard")[];
2
+ export declare const defaultActions: ("bold" | "italic" | "underline" | "strikethrough" | "removeFormat" | "checkboxList" | "insertUnorderedList" | "insertOrderedList" | "blockquote" | "code" | "link" | "indent" | "outdent" | "justifyLeft" | "justifyCenter" | "justifyRight" | "justifyFull" | "undo" | "redo" | "keyboard")[];
3
3
  interface RichToolbarProps {
4
4
  editor?: {
5
5
  current: any;
@@ -40,10 +40,24 @@ const react_native_1 = require("react-native");
40
40
  const actions_1 = require("./actions");
41
41
  exports.defaultActions = [
42
42
  actions_1.actions.keyboard,
43
+ actions_1.actions.undo,
44
+ actions_1.actions.redo,
43
45
  actions_1.actions.setBold,
44
46
  actions_1.actions.setItalic,
45
47
  actions_1.actions.setUnderline,
48
+ actions_1.actions.strikethrough,
49
+ actions_1.actions.removeFormat,
50
+ actions_1.actions.insertUnorderedList,
51
+ actions_1.actions.insertOrderedList,
46
52
  actions_1.actions.checkboxList,
53
+ actions_1.actions.blockquote,
54
+ actions_1.actions.code,
55
+ actions_1.actions.indent,
56
+ actions_1.actions.outdent,
57
+ actions_1.actions.justifyLeft,
58
+ actions_1.actions.justifyCenter,
59
+ actions_1.actions.justifyRight,
60
+ actions_1.actions.justifyFull,
47
61
  actions_1.actions.insertLink,
48
62
  ];
49
63
  // Icons from img folder - direct require now that icons exist
@@ -52,8 +66,22 @@ function getDefaultIcon() {
52
66
  [actions_1.actions.setBold]: require('./img/bold.png'),
53
67
  [actions_1.actions.setItalic]: require('./img/italic.png'),
54
68
  [actions_1.actions.setUnderline]: require('./img/underline.png'),
69
+ [actions_1.actions.strikethrough]: require('./img/strikethrough.png'),
70
+ [actions_1.actions.removeFormat]: require('./img/remove_format.png'),
55
71
  [actions_1.actions.checkboxList]: require('./img/checkbox.png'),
72
+ [actions_1.actions.insertUnorderedList]: require('./img/ul.png'),
73
+ [actions_1.actions.insertOrderedList]: require('./img/ol.png'),
74
+ [actions_1.actions.blockquote]: require('./img/blockquote.png'),
75
+ [actions_1.actions.code]: require('./img/code.png'),
56
76
  [actions_1.actions.insertLink]: require('./img/link.png'),
77
+ [actions_1.actions.indent]: require('./img/indent.png'),
78
+ [actions_1.actions.outdent]: require('./img/outdent.png'),
79
+ [actions_1.actions.justifyLeft]: require('./img/justify_left.png'),
80
+ [actions_1.actions.justifyCenter]: require('./img/justify_center.png'),
81
+ [actions_1.actions.justifyRight]: require('./img/justify_right.png'),
82
+ [actions_1.actions.justifyFull]: require('./img/justify_full.png'),
83
+ [actions_1.actions.undo]: require('./img/undo.png'),
84
+ [actions_1.actions.redo]: require('./img/redo.png'),
57
85
  [actions_1.actions.keyboard]: require('./img/keyboard.png'),
58
86
  };
59
87
  }
@@ -127,7 +155,21 @@ function RichToolbar({ editor, getEditor, actions: actionsProp = exports.default
127
155
  case actions_1.actions.setBold:
128
156
  case actions_1.actions.setItalic:
129
157
  case actions_1.actions.setUnderline:
158
+ case actions_1.actions.strikethrough:
159
+ case actions_1.actions.removeFormat:
130
160
  case actions_1.actions.checkboxList:
161
+ case actions_1.actions.insertUnorderedList:
162
+ case actions_1.actions.insertOrderedList:
163
+ case actions_1.actions.blockquote:
164
+ case actions_1.actions.code:
165
+ case actions_1.actions.indent:
166
+ case actions_1.actions.outdent:
167
+ case actions_1.actions.justifyLeft:
168
+ case actions_1.actions.justifyCenter:
169
+ case actions_1.actions.justifyRight:
170
+ case actions_1.actions.justifyFull:
171
+ case actions_1.actions.undo:
172
+ case actions_1.actions.redo:
131
173
  (_c = editorInstance.showAndroidKeyboard) === null || _c === void 0 ? void 0 : _c.call(editorInstance);
132
174
  (_d = editorInstance.sendAction) === null || _d === void 0 ? void 0 : _d.call(editorInstance, action);
133
175
  break;
package/dist/actions.d.ts CHANGED
@@ -2,8 +2,22 @@ export declare const actions: {
2
2
  readonly setBold: "bold";
3
3
  readonly setItalic: "italic";
4
4
  readonly setUnderline: "underline";
5
+ readonly strikethrough: "strikethrough";
6
+ readonly removeFormat: "removeFormat";
5
7
  readonly checkboxList: "checkboxList";
8
+ readonly insertUnorderedList: "insertUnorderedList";
9
+ readonly insertOrderedList: "insertOrderedList";
10
+ readonly blockquote: "blockquote";
11
+ readonly code: "code";
6
12
  readonly insertLink: "link";
13
+ readonly indent: "indent";
14
+ readonly outdent: "outdent";
15
+ readonly justifyLeft: "justifyLeft";
16
+ readonly justifyCenter: "justifyCenter";
17
+ readonly justifyRight: "justifyRight";
18
+ readonly justifyFull: "justifyFull";
19
+ readonly undo: "undo";
20
+ readonly redo: "redo";
7
21
  readonly keyboard: "keyboard";
8
22
  readonly init: "init";
9
23
  };
package/dist/actions.js CHANGED
@@ -5,8 +5,22 @@ exports.actions = {
5
5
  setBold: 'bold',
6
6
  setItalic: 'italic',
7
7
  setUnderline: 'underline',
8
+ strikethrough: 'strikethrough',
9
+ removeFormat: 'removeFormat',
8
10
  checkboxList: 'checkboxList',
11
+ insertUnorderedList: 'insertUnorderedList',
12
+ insertOrderedList: 'insertOrderedList',
13
+ blockquote: 'blockquote',
14
+ code: 'code',
9
15
  insertLink: 'link',
16
+ indent: 'indent',
17
+ outdent: 'outdent',
18
+ justifyLeft: 'justifyLeft',
19
+ justifyCenter: 'justifyCenter',
20
+ justifyRight: 'justifyRight',
21
+ justifyFull: 'justifyFull',
22
+ undo: 'undo',
23
+ redo: 'redo',
10
24
  keyboard: 'keyboard',
11
- init: 'init'
25
+ init: 'init',
12
26
  };
@@ -10,11 +10,19 @@ function createHTML() {
10
10
  <head>
11
11
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
12
12
  <style>
13
- body { margin:0; font-family: system-ui; }
13
+ html, body {
14
+ margin: 0;
15
+ padding: 0;
16
+ font-family: system-ui;
17
+ min-height: 200px;
18
+ height: 100%;
19
+ }
14
20
  #editor {
15
21
  padding: 12px;
16
- min-height: 100%;
22
+ min-height: 150px;
17
23
  outline: none;
24
+ -webkit-user-select: text;
25
+ user-select: text;
18
26
  }
19
27
  [placeholder]:empty::before {
20
28
  content: attr(placeholder);
@@ -59,35 +67,63 @@ ${(0, contentCSS_1.getContentCSS)()}
59
67
  window.addEventListener('message', e => {
60
68
  const msg = JSON.parse(e.data);
61
69
  const type = msg.type;
62
-
63
- if (type === 'bold') document.execCommand('bold');
64
- else if (type === 'italic') document.execCommand('italic');
65
- else if (type === 'underline') document.execCommand('underline');
66
- else if (type === 'link') {
67
- const url = prompt('Enter link URL:');
68
- if (url) {
69
- const sel = window.getSelection();
70
- const range = sel.getRangeAt(0);
71
- const link = document.createElement('a');
72
- link.href = url;
73
- link.textContent = sel.toString() || url;
74
- range.deleteContents();
75
- range.insertNode(link);
76
- }
77
- }
78
- else if (type === 'checkboxList') {
79
- if (document.queryCommandState('insertOrderedList')) {
70
+
71
+ if (type === 'focus') {
72
+ editor.focus();
73
+ } else if (type === 'blur') {
74
+ editor.blur();
75
+ } else {
76
+ // Restore focus so format commands apply to the editor (important when toolbar tap steals focus)
77
+ editor.focus();
78
+ if (type === 'bold') {
79
+ document.execCommand('bold');
80
+ } else if (type === 'italic') {
81
+ document.execCommand('italic');
82
+ } else if (type === 'underline') {
83
+ document.execCommand('underline');
84
+ } else if (type === 'strikethrough') {
85
+ document.execCommand('strikeThrough');
86
+ } else if (type === 'removeFormat') {
87
+ document.execCommand('removeFormat');
88
+ } else if (type === 'undo') {
89
+ document.execCommand('undo');
90
+ } else if (type === 'redo') {
91
+ document.execCommand('redo');
92
+ } else if (type === 'insertUnorderedList') {
93
+ document.execCommand('insertUnorderedList');
94
+ } else if (type === 'insertOrderedList') {
80
95
  document.execCommand('insertOrderedList');
81
- } else {
96
+ } else if (type === 'checkboxList') {
82
97
  document.execCommand('insertOrderedList');
98
+ } else if (type === 'indent') {
99
+ document.execCommand('indent');
100
+ } else if (type === 'outdent') {
101
+ document.execCommand('outdent');
102
+ } else if (type === 'justifyLeft') {
103
+ document.execCommand('justifyLeft');
104
+ } else if (type === 'justifyCenter') {
105
+ document.execCommand('justifyCenter');
106
+ } else if (type === 'justifyRight') {
107
+ document.execCommand('justifyRight');
108
+ } else if (type === 'justifyFull') {
109
+ document.execCommand('justifyFull');
110
+ } else if (type === 'blockquote') {
111
+ document.execCommand('formatBlock', false, 'blockquote');
112
+ } else if (type === 'code') {
113
+ document.execCommand('formatBlock', false, 'pre');
114
+ } else if (type === 'link') {
115
+ const url = prompt('Enter link URL:');
116
+ if (url) {
117
+ const sel = window.getSelection();
118
+ const range = sel.getRangeAt(0);
119
+ const link = document.createElement('a');
120
+ link.href = url;
121
+ link.textContent = sel.toString() || url;
122
+ range.deleteContents();
123
+ range.insertNode(link);
124
+ }
83
125
  }
84
126
  }
85
- else if (type === 'focus') {
86
- editor.focus();
87
- }
88
- else if (type === 'blur') {
89
- editor.blur();
90
- }
91
127
  });
92
128
  </script>
93
129
  </body>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-rich-text-editor",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "A rich text editor component for React Native",
5
5
  "keywords": [
6
6
  "react-native",