Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.30
1.1 damieng 1: /*
2: This file is part of LON-CAPA.
3:
4: LON-CAPA is free software: you can redistribute it and/or modify
5: it under the terms of the GNU General Public License as published by
6: the Free Software Foundation, either version 3 of the License, or
7: (at your option) any later version.
8:
9: LON-CAPA is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with LON-CAPA. If not, see <http://www.gnu.org/licenses/>.
16: */
17:
18: library loncapa_daxe;
19:
20: import 'dart:async';
21: import 'dart:collection';
22: import 'dart:html' as h;
23: import 'package:daxe/daxe.dart';
24: import 'package:daxe/src/xmldom/xmldom.dart' as x;
25: import 'package:daxe/src/strings.dart';
1.14 damieng 26: import 'package:daxe/src/nodes/nodes.dart' show DNCData, DNText, SimpleTypeControl, ParentUpdatingDNText, DNHiddenP, DNString;
1.1 damieng 27: import 'dart:js' as js;
28:
29: import 'lcd_strings.dart';
30: part 'nodes/lcd_block.dart';
31: part 'nodes/lcd_parameter.dart';
32: part 'nodes/tex_mathjax.dart';
33: part 'nodes/lm.dart';
1.14 damieng 34: part 'nodes/chem.dart';
1.2 damieng 35: part 'nodes/numerical_response.dart';
1.3 damieng 36: part 'nodes/formula_response.dart';
37: part 'nodes/string_response.dart';
1.11 damieng 38: part 'nodes/essay_response.dart';
39: part 'nodes/textfield.dart';
1.1 damieng 40: part 'nodes/radio_response.dart';
41: part 'nodes/radio_foilgroup.dart';
42: part 'nodes/radio_foil.dart';
43: part 'nodes/option_response.dart';
44: part 'nodes/option_foilgroup.dart';
45: part 'nodes/option_foil.dart';
1.6 damieng 46: part 'nodes/match_response.dart';
47: part 'nodes/match_foilgroup.dart';
48: part 'nodes/match_foil.dart';
49: part 'nodes/itemgroup.dart';
50: part 'nodes/match_item.dart';
1.1 damieng 51: part 'nodes/rank_response.dart';
52: part 'nodes/rank_foilgroup.dart';
53: part 'nodes/rank_foil.dart';
1.10 damieng 54: part 'nodes/math_response.dart';
55: part 'nodes/math_answer.dart';
1.13 damieng 56: part 'nodes/organic_response.dart';
57: part 'nodes/jsme_dialog.dart';
58: part 'nodes/reaction_response.dart';
1.5 damieng 59: part 'nodes/textline.dart';
1.1 damieng 60: part 'nodes/hintgroup.dart';
61: part 'nodes/simple_ui_text.dart';
62: part 'nodes/script_block.dart';
1.2 damieng 63: part 'nodes/simple_ui_exception.dart';
1.7 damieng 64: part 'nodes/gnuplot.dart';
1.22 damieng 65: part 'nodes/organic_structure.dart';
1.7 damieng 66: part 'nodes/fake_attribute_element.dart';
67: part 'nodes/plot_axis.dart';
68: part 'nodes/plot_curve.dart';
1.17 damieng 69: part 'nodes/section.dart';
70: part 'nodes/h1.dart';
1.1 damieng 71: part 'lcd_button.dart';
1.28 damieng 72: part 'lcd_insert_panel.dart';
1.1 damieng 73:
1.18 damieng 74: List<String> knownSectionRoles = ['activity', 'advice', 'bibliography', 'citation',
75: 'conclusion', 'definition', 'demonstration', 'example',
76: 'explanation', 'introduction', 'method', 'more_information',
77: 'objectives', 'prerequisites', 'remark', 'reminder',
78: 'summary', 'syntax', 'warning'];
79:
1.1 damieng 80:
81: void main() {
82: NodeFactory.addCoreDisplayTypes();
83:
84: addDisplayType('lcdblock',
85: (x.Element ref) => new LCDBlock.fromRef(ref),
86: (x.Node node, DaxeNode parent) => new LCDBlock.fromNode(node, parent)
87: );
88:
89: addDisplayType('texmathjax',
90: (x.Element ref) => new TeXMathJax.fromRef(ref),
91: (x.Node node, DaxeNode parent) => new TeXMathJax.fromNode(node, parent)
92: );
93:
94: addDisplayType('lm',
95: (x.Element ref) => new Lm.fromRef(ref),
96: (x.Node node, DaxeNode parent) => new Lm.fromNode(node, parent)
97: );
98:
1.14 damieng 99: addDisplayType('chem',
100: (x.Element ref) => new Chem.fromRef(ref),
101: (x.Node node, DaxeNode parent) => new Chem.fromNode(node, parent)
102: );
103:
1.1 damieng 104: addDisplayType('script',
1.27 damieng 105: (x.Element ref) => new ScriptBlock.fromRef(ref),
106: (x.Node node, DaxeNode parent) => new ScriptBlock.fromNode(node, parent)
1.1 damieng 107: );
108:
109: addDisplayType('parameter',
110: (x.Element ref) => new LCDParameter.fromRef(ref),
111: (x.Node node, DaxeNode parent) => new LCDParameter.fromNode(node, parent)
112: );
113:
1.2 damieng 114: addDisplayType('numericalresponse',
115: (x.Element ref) => new NumericalResponse.fromRef(ref),
116: (x.Node node, DaxeNode parent) => new NumericalResponse.fromNode(node, parent)
117: );
118:
1.3 damieng 119: addDisplayType('formularesponse',
120: (x.Element ref) => new FormulaResponse.fromRef(ref),
121: (x.Node node, DaxeNode parent) => new FormulaResponse.fromNode(node, parent)
122: );
123:
124: addDisplayType('stringresponse',
125: (x.Element ref) => new StringResponse.fromRef(ref),
126: (x.Node node, DaxeNode parent) => new StringResponse.fromNode(node, parent)
127: );
128:
1.11 damieng 129: addDisplayType('essayresponse',
130: (x.Element ref) => new EssayResponse.fromRef(ref),
131: (x.Node node, DaxeNode parent) => new EssayResponse.fromNode(node, parent)
132: );
133:
1.1 damieng 134: addDisplayType('radioresponse',
135: (x.Element ref) => new RadioResponse.fromRef(ref),
136: (x.Node node, DaxeNode parent) => new RadioResponse.fromNode(node, parent)
137: );
138:
139: addDisplayType('optionresponse',
140: (x.Element ref) => new OptionResponse.fromRef(ref),
141: (x.Node node, DaxeNode parent) => new OptionResponse.fromNode(node, parent)
142: );
143:
1.6 damieng 144: addDisplayType('matchresponse',
145: (x.Element ref) => new MatchResponse.fromRef(ref),
146: (x.Node node, DaxeNode parent) => new MatchResponse.fromNode(node, parent)
147: );
148:
1.1 damieng 149: addDisplayType('rankresponse',
150: (x.Element ref) => new RankResponse.fromRef(ref),
151: (x.Node node, DaxeNode parent) => new RankResponse.fromNode(node, parent)
152: );
153:
1.10 damieng 154: addDisplayType('mathresponse',
155: (x.Element ref) => new MathResponse.fromRef(ref),
156: (x.Node node, DaxeNode parent) => new MathResponse.fromNode(node, parent)
157: );
158:
1.13 damieng 159: addDisplayType('organicresponse',
160: (x.Element ref) => new OrganicResponse.fromRef(ref),
161: (x.Node node, DaxeNode parent) => new OrganicResponse.fromNode(node, parent)
162: );
163:
164: addDisplayType('reactionresponse',
165: (x.Element ref) => new ReactionResponse.fromRef(ref),
166: (x.Node node, DaxeNode parent) => new ReactionResponse.fromNode(node, parent)
167: );
168:
1.1 damieng 169: addDisplayType('foilgroup',
170: (x.Element ref) {
171: if (ref.getAttribute('type') == 'radiobuttonresponse--foilgroup')
172: return new RadioFoilgroup.fromRef(ref);
173: else if (ref.getAttribute('type') == 'optionresponse--foilgroup')
174: return new OptionFoilgroup.fromRef(ref);
1.6 damieng 175: else if (ref.getAttribute('type') == 'matchresponse--foilgroup')
176: return new MatchFoilgroup.fromRef(ref);
1.1 damieng 177: else if (ref.getAttribute('type') == 'rankresponse--foilgroup')
178: return new RankFoilgroup.fromRef(ref);
179: return new LCDBlock.fromRef(ref);
180: },
181: (x.Node node, DaxeNode parent) {
182: if (parent is RadioResponse)
183: return new RadioFoilgroup.fromNode(node, parent);
184: else if (parent is OptionResponse)
185: return new OptionFoilgroup.fromNode(node, parent);
1.6 damieng 186: else if (parent is MatchResponse)
187: return new MatchFoilgroup.fromNode(node, parent);
1.1 damieng 188: else if (parent is RankResponse)
189: return new RankFoilgroup.fromNode(node, parent);
190: return new LCDBlock.fromNode(node, parent);
191: }
192: );
193:
194: addDisplayType('foil',
195: (x.Element ref) {
196: if (ref.getAttribute('type') == 'radiobuttonresponse--foil')
197: return new RadioFoil.fromRef(ref);
198: else if (ref.getAttribute('type') == 'optionresponse--foil')
199: return new OptionFoil.fromRef(ref);
1.6 damieng 200: else if (ref.getAttribute('type') == 'matchresponse--foil')
201: return new MatchFoil.fromRef(ref);
1.1 damieng 202: else if (ref.getAttribute('type') == 'rankresponse--foil')
203: return new RankFoil.fromRef(ref);
204: return new LCDBlock.fromRef(ref);
205: },
206: (x.Node node, DaxeNode parent) {
207: if (parent is RadioFoilgroup)
208: return new RadioFoil.fromNode(node, parent);
1.19 damieng 209: else if (parent is OptionFoilgroup ||
210: (parent.parent != null && parent.parent is OptionFoilgroup))
1.1 damieng 211: return new OptionFoil.fromNode(node, parent);
1.19 damieng 212: else if (parent is MatchFoilgroup ||
213: (parent.parent != null && parent.parent is MatchFoilgroup))
1.6 damieng 214: return new MatchFoil.fromNode(node, parent);
1.1 damieng 215: else if (parent is RankFoilgroup)
216: return new RankFoil.fromNode(node, parent);
217: return new LCDBlock.fromNode(node, parent);
218: }
219: );
220:
1.6 damieng 221: addDisplayType('itemgroup',
222: (x.Element ref) => new Itemgroup.fromRef(ref),
223: (x.Node node, DaxeNode parent) => new Itemgroup.fromNode(node, parent)
224: );
225:
226: addDisplayType('matchitem',
227: (x.Element ref) => new MatchItem.fromRef(ref),
228: (x.Node node, DaxeNode parent) => new MatchItem.fromNode(node, parent)
229: );
230:
1.5 damieng 231: addDisplayType('textline',
232: (x.Element ref) => new Textline.fromRef(ref),
233: (x.Node node, DaxeNode parent) => new Textline.fromNode(node, parent)
234: );
235:
1.11 damieng 236: addDisplayType('textfield',
237: (x.Element ref) => new Textfield.fromRef(ref),
238: (x.Node node, DaxeNode parent) => new Textfield.fromNode(node, parent)
239: );
240:
1.1 damieng 241: addDisplayType('hintgroup',
242: (x.Element ref) => new Hintgroup.fromRef(ref),
243: (x.Node node, DaxeNode parent) => new Hintgroup.fromNode(node, parent)
244: );
245:
1.10 damieng 246: addDisplayType('answer',
247: (x.Element ref) {
248: if (ref.getAttribute('type') == 'mathresponse--answer')
249: return new MathAnswer.fromRef(ref);
1.27 damieng 250: else if (ref.getAttribute('type') == 'caparesponse--answer')
251: return new LCDBlock.fromRef(ref);
252: else
253: return new ScriptBlock.fromRef(ref);
1.10 damieng 254: },
255: (x.Node node, DaxeNode parent) {
256: if (parent is MathResponse)
257: return new MathAnswer.fromNode(node, parent);
1.27 damieng 258: else if (parent != null && parent.ref != null &&
259: parent.ref.getAttribute('type') == 'caparesponse--answergroup')
260: return new LCDBlock.fromNode(node, parent);
261: else
262: return new ScriptBlock.fromNode(node, parent);
1.10 damieng 263: }
264: );
265:
1.7 damieng 266: addDisplayType('gnuplot',
267: (x.Element ref) => new Gnuplot.fromRef(ref),
268: (x.Node node, DaxeNode parent) => new Gnuplot.fromNode(node, parent)
269: );
270:
1.22 damieng 271: addDisplayType('organicstructure',
272: (x.Element ref) => new OrganicStructure.fromRef(ref),
273: (x.Node node, DaxeNode parent) => new OrganicStructure.fromNode(node, parent)
274: );
275:
1.7 damieng 276: addDisplayType('fakeattribute',
277: (x.Element ref) {
278: if (doc.cfg.elementName(ref) != 'title' || ref.getAttribute('type') == 'lonplot--title')
279: return new FakeAttributeElement.fromRef(ref);
280: return new LCDBlock.fromRef(ref);
281: },
282: (x.Node node, DaxeNode parent) {
283: if (parent is Gnuplot || parent is PlotCurve)
284: return new FakeAttributeElement.fromNode(node, parent);
285: return new LCDBlock.fromNode(node, parent);
286: }
287: );
288:
289: addDisplayType('plotaxis',
290: (x.Element ref) => new PlotAxis.fromRef(ref),
291: (x.Node node, DaxeNode parent) => new PlotAxis.fromNode(node, parent)
292: );
293:
294: addDisplayType('plotcurve',
295: (x.Element ref) => new PlotCurve.fromRef(ref),
296: (x.Node node, DaxeNode parent) => new PlotCurve.fromNode(node, parent)
297: );
298:
1.17 damieng 299: addDisplayType('section',
300: (x.Element ref) => new Section.fromRef(ref),
301: (x.Node node, DaxeNode parent) => new Section.fromNode(node, parent)
302: );
303:
304: addDisplayType('h1',
305: (x.Element ref) => new H1.fromRef(ref),
306: (x.Node node, DaxeNode parent) => new H1.fromNode(node, parent)
307: );
308:
1.28 damieng 309: InsertPanel insertP = new LCDInsertPanel();
310: LeftPanel left = new LeftPanel(insert:insertP);
1.29 damieng 311: ActionFunction saveFunction= () {
312: doc.save().then((_) {
313: // h.window.alert(Strings.get('save.success'));
314: // update the preview iframe
315: // JS: parent.getElementById('preview').location.reload()
316: // cannot be done in pure Dart
317: js.JsObject parent = new js.JsObject.fromBrowserObject(js.context['window'])['parent'];
318: js.JsObject preview = parent['document'].callMethod('getElementById', ['preview']);
319: preview['src'] = preview['src'];
320: }, onError: (DaxeException ex) {
321: h.window.alert(Strings.get('save.error') + ': ' + ex.message);
322: });
323: };
1.1 damieng 324: Future.wait([Strings.load(), LCDStrings.load(), _readTemplates('templates.xml')]).then((List responses) {
1.29 damieng 325: initDaxe(left:left, saveFunction:saveFunction).then((v) {
1.28 damieng 326: // change insert panel
1.1 damieng 327: // add things to the toolbar
328: ToolbarMenu sectionMenu = _makeSectionMenu();
329: if (sectionMenu != null)
330: page.toolbar.add(sectionMenu);
1.24 damieng 331: ToolbarBox mathBox;
332: if (doc.saveURL != null)
1.30 ! damieng 333: mathBox = page.toolbar.items[5];
1.24 damieng 334: else
1.30 ! damieng 335: mathBox = page.toolbar.items[4];
1.1 damieng 336: x.Element texRef = doc.cfg.elementReference('m');
337: if (texRef != null) {
338: ToolbarButton texButton = new ToolbarButton(
1.24 damieng 339: LCDStrings.get('insert_m'), 'images/tex.png',
1.1 damieng 340: () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate,
341: data:new ToolbarStyleInfo([texRef], null, null));
1.23 damieng 342: mathBox.add(texButton);
1.1 damieng 343: }
1.24 damieng 344: x.Element lmRef = doc.cfg.elementReference('lm');
345: if (lmRef != null) {
346: ToolbarButton lmButton = new ToolbarButton(
347: LCDStrings.get('insert_lm'), 'packages/daxe/images/toolbar/equation.png',
348: () => doc.insertNewNode(lmRef, 'element'), Toolbar.insertButtonUpdate,
349: data:new ToolbarStyleInfo([lmRef], null, null));
350: mathBox.add(lmButton);
351: }
1.1 damieng 352: h.Element tbh = h.querySelector('.toolbar');
353: tbh.replaceWith(page.toolbar.html());
354: page.adjustPositionsUnderToolbar();
355: page.updateAfterPathChange();
356: // add things to the menubar
357: if (responses[2] is x.Document) {
358: // at this point the menubar html is already in the document, so we have to fix the HTML
359: h.Element menubarDiv = h.document.getElementsByClassName('menubar')[0];
360: Menu m = _makeTemplatesMenu(responses[2]);
1.26 damieng 361: if (m != null)
362: page.mbar.add(m);
1.1 damieng 363: menubarDiv.append(page.mbar.createMenuDiv(m));
364: page.updateAfterPathChange();
365: } else
366: print("Error reading templates file, could not build the menu.");
1.15 damieng 367: }).catchError((e) {
1.26 damieng 368: h.document.body.appendText('Initialization error: ' + (e is String ? e : e.toString()));
1.1 damieng 369: });
1.15 damieng 370: }).catchError((e) {
1.20 damieng 371: h.document.body.appendText('Initialization error: ' + (e is String ? e : e.toString()));
1.1 damieng 372: });
373: }
374:
375: ToolbarMenu _makeSectionMenu() {
1.18 damieng 376: // sort section roles by title
377: knownSectionRoles.sort((String role1, String role2) => LCDStrings.get(role1).compareTo(LCDStrings.get(role2)));
378:
1.1 damieng 379: Menu menu = new Menu(LCDStrings.get('Section'));
380: List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
381: if (sectionRefs == null || sectionRefs.length == 0)
382: return(null);
383: x.Element h1Ref = doc.cfg.elementReference('h1');
1.18 damieng 384: for (String role in knownSectionRoles) {
1.1 damieng 385: MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
386: data:new ToolbarStyleInfo(sectionRefs, null, null));
387: menuItem.action = () {
388: ToolbarStyleInfo info = menuItem.data;
389: x.Element sectionRef = info.validRef;
1.18 damieng 390: Section section = new Section.fromRef(sectionRef);
1.1 damieng 391: section.state = 1;
392: section.setAttribute('class', 'role-' + role);
1.17 damieng 393: H1 h1 = NodeFactory.create(h1Ref);
1.1 damieng 394: h1.state = 1;
1.18 damieng 395: h1.appendChild(new DNText(LCDStrings.get(role)));
1.9 damieng 396: section.appendChild(h1);
397: x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
1.18 damieng 398: DNHiddenP p = new DNHiddenP.fromRef(hiddenp);
1.9 damieng 399: section.appendChild(p);
1.17 damieng 400: section.setupSimpleUI();
1.1 damieng 401: if (doc.insert2(section, page.getSelectionStart())) {
1.18 damieng 402: page.cursor.moveTo(new Position(p, 0));
1.1 damieng 403: page.updateAfterPathChange();
404: }
405: };
406: menu.add(menuItem);
407: }
408: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
409: return(tbmenu);
410: }
411:
412: Future<x.Document> _readTemplates(String templatesPath) {
413: x.DOMParser dp = new x.DOMParser();
414: return(dp.parseFromURL(templatesPath));
415: }
416:
417: Menu _makeTemplatesMenu(x.Document templatesDoc) {
1.26 damieng 418: List<x.Element> problemRefs = doc.cfg.elementReferences('problem');
419: if (problemRefs == null || problemRefs.length == 0)
420: return(null);
1.1 damieng 421: Menu menu = new Menu(LCDStrings.get('Templates'));
422: x.Element templates = templatesDoc.documentElement;
423: for (x.Node child in templates.childNodes) {
424: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
425: menu.add(_makeMenu(child));
426: }
427: }
428: return(menu);
429: }
430:
431: Menu _makeMenu(x.Element el) {
432: String locale = LCDStrings.systemLocale;
433: String defaultLocale = LCDStrings.defaultLocale;
434: String title;
435: for (x.Node child in el.childNodes) {
436: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
437: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
438: if ((child as x.Element).getAttribute('lang') == locale) {
439: title = child.firstChild.nodeValue;
440: break;
441: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
442: title = child.firstChild.nodeValue;
443: }
444: }
445: }
446: }
447: if (title == null)
448: title = '?';
449: Menu menu = new Menu(title);
450: for (x.Node child in el.childNodes) {
451: if (child.nodeType == x.Node.ELEMENT_NODE) {
452: if (child.nodeName == 'menu') {
453: menu.add(_makeMenu(child));
454: } else if (child.nodeName == 'item') {
455: menu.add(_makeItem(child));
456: }
457: }
458: }
459: return(menu);
460: }
461:
462: MenuItem _makeItem(x.Element item) {
463: String locale = LCDStrings.systemLocale;
464: String defaultLocale = LCDStrings.defaultLocale;
465: String path, type, title, help;
466: for (x.Node child in item.childNodes) {
467: if (child.nodeType == x.Node.ELEMENT_NODE) {
468: if (child.nodeName == 'title') {
469: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
470: if ((child as x.Element).getAttribute('lang') == locale) {
471: title = child.firstChild.nodeValue;
472: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
473: title = child.firstChild.nodeValue;
474: }
475: }
476: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
477: path = child.firstChild.nodeValue;
478: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
479: type = child.firstChild.nodeValue;
480: } else if (child.nodeName == 'help') {
481: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
482: if ((child as x.Element).getAttribute('lang') == locale) {
483: help = child.firstChild.nodeValue;
484: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
485: help = child.firstChild.nodeValue;
486: }
487: }
488: }
489: }
490: }
491: if (type == null) {
492: print("Warning: missing type for template $title\n");
493: type = 'problem';
494: }
495: x.Element refElement = doc.cfg.elementReference(type);
496: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
497: if (help != null)
498: menuItem.toolTipText = help;
499: return menuItem;
500: }
501:
502: void _insertTemplate(String filePath) {
503: try {
504: x.DOMParser dp = new x.DOMParser();
505: dp.parseFromURL(filePath).then((x.Document templateDoc) {
506: x.Element root = templateDoc.documentElement;
507: if (root == null)
508: return;
509: doc.removeWhitespace(root);
510: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
1.27 damieng 511: if (dnRoot is LCDBlock && dnRoot.attributes.length > 0)
512: dnRoot.state = 0; // make attributes editable for the template root
1.1 damieng 513: UndoableEdit edit;
514: Position pos = page.getSelectionStart();
515: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
516: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12 damieng 517: else {
518: DaxeNode parent = pos.dn;
519: if (parent is DNText)
520: parent = parent.parent;
521: if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
522: // The node must be inserted outside of the paragraph.
523: // If there is something in the paragraph to the right of the cursor, it must be
524: // moved into a new paragraph after the inserted node.
525: // (as in DaxeDocument.insert2)
526: DNHiddenP p = parent;
527: edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
528: Position pend = new Position(p, p.offsetLength);
529: pend.moveInsideTextNodeIfPossible();
530: if (pos < pend) {
531: DaxeNode clone = doc.cloneBetween(pos, pend);
532: edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
533: DNHiddenP newp = NodeFactory.create(p.ref);
534: edit.addSubEdit(new UndoableEdit.insertNode(
535: new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
536: edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
537: }
538: edit.addSubEdit(new UndoableEdit.insertNode(
539: new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
540: } else
541: edit = new UndoableEdit.insertNode(pos, dnRoot);
542: }
1.1 damieng 543: doc.doNewEdit(edit);
544: page.updateAfterPathChange();
1.8 damieng 545: if (dnRoot.nodeName != 'loncapa')
546: page.scrollToNode(dnRoot);
1.1 damieng 547: });
548: } on x.DOMException catch(ex) {
549: h.window.alert(ex.toString());
550: }
551: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>