mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2025-07-26 10:49:49 +02:00
editor control
This commit is contained in:
32
OpenPage/coffees/dialogs.coffee
Normal file
32
OpenPage/coffees/dialogs.coffee
Normal file
@ -0,0 +1,32 @@
|
||||
class HyperLinkDialog extends this.OS.GUI.BasicDialog
|
||||
constructor: () ->
|
||||
super "HyperLinkDialog", {
|
||||
tags: [
|
||||
{ tag: "afx-label", att: 'text="__(Text)" data-height="23" class="header"' },
|
||||
{ tag: "input", att: 'data-height="30"' },
|
||||
{ tag: "afx-label", att: 'text="__(Link)" data-height="23" class="header"' },
|
||||
{ tag: "input", att: 'data-height="30"' },
|
||||
{ tag: "div", att: ' data-height="5"' }
|
||||
],
|
||||
width: 350,
|
||||
height: 150,
|
||||
resizable: false,
|
||||
buttons: [
|
||||
{
|
||||
label: "Ok", onclick: (d) ->
|
||||
data =
|
||||
text: (d.find "content1").value,
|
||||
link: (d.find "content3").value,
|
||||
readonly: d.data.readonly,
|
||||
action: d.data.action
|
||||
d.handler data if d.handler
|
||||
d.quit()
|
||||
},
|
||||
{ label: "__(Cancel)", onclick: (d) -> d.quit() }
|
||||
],
|
||||
filldata: (d) ->
|
||||
return unless d.data
|
||||
(d.find "content1").value = d.data.text
|
||||
(d.find "content3").value = d.data.link
|
||||
$(d.find "content1").prop('disabled', d.data.readonly)
|
||||
}
|
@ -3,19 +3,64 @@ class OpenPage extends this.OS.GUI.BaseApplication
|
||||
super "OpenPage", args
|
||||
|
||||
main: () ->
|
||||
# load session class
|
||||
if not OpenPage.editorSession
|
||||
require ["webodf/editor/EditorSession"], (ES) ->
|
||||
OpenPage.EditorSession = ES
|
||||
@eventSubscriptions = new core.EventSubscriptions()
|
||||
@initToolbox()
|
||||
@initCanvas()
|
||||
@canvas.load "#{@_api.handler.get}/home://welcome.odt"
|
||||
|
||||
initToolbox: () ->
|
||||
me = @
|
||||
@basictool =
|
||||
undo: @find("btundo"),
|
||||
redo: @find("btredo"),
|
||||
bold: @find("btbold"),
|
||||
italic:@find("btitalic"),
|
||||
underline:@find("btunderline"),
|
||||
strike: @find("btstrike"),
|
||||
note: @find("btnote"),
|
||||
link: @find("btlink"),
|
||||
unlink: @find("btunlink"),
|
||||
image:@find("btimage"),
|
||||
ac: @find("btac"),
|
||||
al: @find("btal"),
|
||||
ar: @find("btar"),
|
||||
aj: @find("btaj"),
|
||||
indent: @find("btindent"),
|
||||
outdent: @find("btoutdent"),
|
||||
fonts: @find("font-list"),
|
||||
fontsize: @find("font-size")
|
||||
fn = (name, el) ->
|
||||
if name is "fonts"
|
||||
act = "onlistselect"
|
||||
else if name is "fontsize"
|
||||
act = "onchange"
|
||||
else
|
||||
act = "onbtclick"
|
||||
el.set act, (e) ->
|
||||
return unless me.directFormattingCtl
|
||||
return unless me[name]
|
||||
me[name](e)
|
||||
me.editorFocus()
|
||||
for name, el of @basictool
|
||||
fn name, el
|
||||
|
||||
initCanvas: () ->
|
||||
el = @find "odfcanvas"
|
||||
me = @
|
||||
el.setAttribute "translate", "no"
|
||||
el.classList.add "notranslate"
|
||||
@eventNotifier = new core.EventNotifier [
|
||||
"unknown-error",
|
||||
"documentModifiedChanged",
|
||||
"metadataChanged"
|
||||
]
|
||||
userid = "localuser"
|
||||
require ["webodf/editor/EditorSession"], (ES) ->
|
||||
OpenPage.EditorSession = ES
|
||||
@userid = "localuser"
|
||||
@canvas = new odf.OdfCanvas(el)
|
||||
@documentChanged = (e) ->
|
||||
console.log e
|
||||
@metaChanged = (e) ->
|
||||
console.log e
|
||||
@textStylingChanged = (e) ->
|
||||
me.updateToolbar e
|
||||
#@canvas.enableAnnotations(true, true)
|
||||
@canvas.addListener "statereadychange", ()->
|
||||
me.session = new ops.Session(me.canvas)
|
||||
@ -24,7 +69,7 @@ class OpenPage extends this.OS.GUI.BaseApplication
|
||||
caretAvatarsInitiallyVisible: false,
|
||||
caretBlinksOnRangeSelect: true
|
||||
|
||||
me.editorSession = new OpenPage.EditorSession(me.session,userid, {
|
||||
me.editorSession = new OpenPage.EditorSession(me.session,me.userid, {
|
||||
viewOptions: viewOptions,
|
||||
directTextStylingEnabled: true,
|
||||
directParagraphStylingEnabled: true,
|
||||
@ -36,25 +81,181 @@ class OpenPage extends this.OS.GUI.BaseApplication
|
||||
zoomingEnabled: true,
|
||||
reviewModeEnabled: false
|
||||
})
|
||||
# basic format
|
||||
me.directFormattingCtl = me.editorSession.sessionController.getDirectFormattingController()
|
||||
me.directFormattingCtl.subscribe gui.DirectFormattingController.textStylingChanged, me.textStylingChanged
|
||||
me.directFormattingCtl.subscribe gui.DirectFormattingController.paragraphStylingChanged, me.textStylingChanged
|
||||
# hyper link controller
|
||||
me.hyperlinkController = me.editorSession.sessionController.getHyperlinkController()
|
||||
me.eventSubscriptions.addFrameSubscription me.editorSession, OpenPage.EditorSession.signalCursorMoved, ()-> me.updateHyperlinkButtons()
|
||||
me.eventSubscriptions.addFrameSubscription me.editorSession, OpenPage.EditorSession.signalParagraphChanged, ()-> me.updateHyperlinkButtons()
|
||||
me.eventSubscriptions.addFrameSubscription me.editorSession, OpenPage.EditorSession.signalParagraphStyleModified, ()-> me.updateHyperlinkButtons()
|
||||
|
||||
me.editorSession.sessionController.setUndoManager new gui.TrivialUndoManager()
|
||||
me.editorSession.sessionController.getUndoManager().subscribe gui.UndoManager.signalDocumentModifiedChanged, (mod) ->
|
||||
me.eventNotifier.emit "documentModifiedChanged", mod
|
||||
me.editorSession.sessionController.getMetadataController().subscribe gui.MetadataController.signalMetadataChanged, (changes) ->
|
||||
me.eventNotifier.emit "metadataChanged", changes
|
||||
me.editorSession.sessionController.getUndoManager().subscribe gui.UndoManager.signalDocumentModifiedChanged, me.documentChanged
|
||||
me.editorSession.sessionController.getMetadataController().subscribe gui.MetadataController.signalMetadataChanged, me.metaChanged
|
||||
op = new ops.OpAddMember()
|
||||
op.init {
|
||||
memberid: userid,
|
||||
memberid: me.userid,
|
||||
setProperties:{
|
||||
"fullName": "Xuan Sang LE",
|
||||
"color": "blue"
|
||||
}
|
||||
}
|
||||
me.session.enqueue([op])
|
||||
me.initFontList me.editorSession.getDeclaredFonts()
|
||||
me.editorSession.sessionController.insertLocalCursor()
|
||||
me.editorSession.sessionController.startEditing()
|
||||
me.editorSession.sessionController.getEventManager().focus()
|
||||
@canvas.load "#{@_api.handler.get}/home://Downloads/welcome.odt"
|
||||
@eventNotifier.subscribe "documentModifiedChanged", (d) ->
|
||||
console.log "document is modified"
|
||||
#console.log me.editorSession.getDeclaredFonts()
|
||||
#
|
||||
|
||||
initFontList: (list) ->
|
||||
v.text = v.name for v in list
|
||||
@basictool.fonts.set "items", list
|
||||
|
||||
updateToolbar: (changes) ->
|
||||
# basic style
|
||||
(@basictool.bold.set "selected", changes.isBold) if changes.hasOwnProperty 'isBold'
|
||||
(@basictool.italic.set "selected", changes.isItalic) if changes.hasOwnProperty 'isItalic'
|
||||
(@basictool.underline.set "selected", changes.hasUnderline) if changes.hasOwnProperty 'hasUnderline'
|
||||
(@basictool.strike.set "selected", changes.hasStrikeThrough) if changes.hasOwnProperty 'hasStrikeThrough'
|
||||
(@basictool.fontsize.set "value", changes.fontSize) if changes.hasOwnProperty "fontSize"
|
||||
@selectFont changes.fontName if changes.hasOwnProperty "fontName"
|
||||
#pharagraph style
|
||||
@basictool.al.set "selected", changes.isAlignedLeft if changes.hasOwnProperty "isAlignedLeft"
|
||||
@basictool.ar.set "selected", changes.isAlignedRight if changes.hasOwnProperty "isAlignedRight"
|
||||
@basictool.ac.set "selected", changes.isAlignedCenter if changes.hasOwnProperty "isAlignedCenter"
|
||||
@basictool.aj.set "selected", changes.isAlignedJustified if changes.hasOwnProperty "isAlignedJustified"
|
||||
|
||||
updateHyperlinkButtons: (e) ->
|
||||
selectedLinks = @editorSession.getSelectedHyperlinks()
|
||||
@basictool.unlink.set "enable", selectedLinks.length > 0
|
||||
|
||||
selectFont: (name) ->
|
||||
items = @basictool.fonts.get "items"
|
||||
item = i for v, i in items when v.name is name
|
||||
@basictool.fonts.set "selected", item
|
||||
|
||||
editorFocus: () ->
|
||||
@editorSession.sessionController.getEventManager().focus()
|
||||
|
||||
bold: (e) ->
|
||||
#console.log @, e
|
||||
@directFormattingCtl.setBold (not @basictool.bold.get "selected")
|
||||
|
||||
italic: (e) ->
|
||||
@directFormattingCtl.setItalic (not @basictool.italic.get "selected")
|
||||
|
||||
underline: (e) ->
|
||||
@directFormattingCtl.setHasUnderline (not @basictool.underline.get "selected")
|
||||
|
||||
strike: (e) ->
|
||||
@directFormattingCtl.setHasStrikethrough (not @basictool.strike.get "selected")
|
||||
|
||||
fonts: (e) ->
|
||||
@directFormattingCtl.setFontName e.data.name
|
||||
|
||||
fontsize: (e) ->
|
||||
@directFormattingCtl.setFontSize e
|
||||
|
||||
al: (e) ->
|
||||
@directFormattingCtl.alignParagraphLeft()
|
||||
|
||||
ar: (e) ->
|
||||
@directFormattingCtl.alignParagraphRight()
|
||||
|
||||
ac: (e) ->
|
||||
@directFormattingCtl.alignParagraphCenter()
|
||||
|
||||
aj: (e) ->
|
||||
@directFormattingCtl.alignParagraphJustified()
|
||||
|
||||
indent: (e) ->
|
||||
@directFormattingCtl.indent()
|
||||
|
||||
outdent: (e) ->
|
||||
@directFormattingCtl.outdent()
|
||||
|
||||
link: (e) ->
|
||||
# get the link first
|
||||
me = @
|
||||
textSerializer = new odf.TextSerializer()
|
||||
selection = @editorSession.getSelectedRange()
|
||||
linksInSelection = @editorSession.getSelectedHyperlinks()
|
||||
linkTarget = if linksInSelection[0] then odf.OdfUtils.getHyperlinkTarget(linksInSelection[0]) else "http://"
|
||||
data =
|
||||
link: linkTarget,
|
||||
text: "",
|
||||
readonly: true,
|
||||
action: "new"
|
||||
if selection and selection.collapsed and linksInSelection.length == 1
|
||||
# selection is collapsed within a single link
|
||||
# text in this case is read only
|
||||
data.text = textSerializer.writeToString linksInSelection[0]
|
||||
data.action = "edit"
|
||||
else if selection and !selection.collapsed
|
||||
# user select part of link or a block of text
|
||||
# user convert a selection to a link
|
||||
data.text = textSerializer.writeToString selection.cloneContents()
|
||||
else
|
||||
data.readonly = false
|
||||
@openDialog new HyperLinkDialog(), (d) ->
|
||||
selectionController = me.editorSession.sessionController.getSelectionController()
|
||||
if d.readonly
|
||||
# edit the existing link
|
||||
if d.action is "edit"
|
||||
selectedLinkRange = selection.cloneRange()
|
||||
selectedLinkRange.selectNode(linksInSelection[0])
|
||||
selectionController.selectRange(selectedLinkRange, true)
|
||||
me.hyperlinkController.removeHyperlinks()
|
||||
me.hyperlinkController.addHyperlink d.link
|
||||
else
|
||||
me.hyperlinkController.addHyperlink d.link, d.text
|
||||
linksInSelection = me.editorSession.getSelectedHyperlinks()
|
||||
selectedLinkRange = selection.cloneRange()
|
||||
selectedLinkRange.selectNode(linksInSelection[0])
|
||||
selectionController.selectRange(selectedLinkRange, true)
|
||||
, "__(Insert/edit link)", data
|
||||
|
||||
unlink: (e) ->
|
||||
@hyperlinkController.removeHyperlinks()
|
||||
|
||||
closeDocument: () ->
|
||||
# finish editing
|
||||
return unless @editorSession and @session
|
||||
me = @
|
||||
@eventSubscriptions.unsubscribeAll()
|
||||
@editorSession.sessionController.endEditing()
|
||||
@editorSession.sessionController.removeLocalCursor()
|
||||
# remove user
|
||||
op = new ops.OpRemoveMember()
|
||||
op.init {
|
||||
memberid: @userid
|
||||
}
|
||||
@session.enqueue [op]
|
||||
# close the session
|
||||
@session.close (e) ->
|
||||
return (me.error "Cannot close session " + e) if e
|
||||
me.editorSession.sessionController.getMetadataController().unsubscribe gui.MetadataController.signalMetadataChanged, me.metaChanged
|
||||
me.editorSession.sessionController.getUndoManager().unsubscribe gui.UndoManager.signalDocumentModifiedChanged, me.documentChanged
|
||||
me.directFormattingCtl.unsubscribe gui.DirectFormattingController.textStylingChanged, me.textStylingChanged
|
||||
me.directFormattingCtl.unsubscribe gui.DirectFormattingController.paragraphStylingChanged, me.textStylingChanged
|
||||
# destry editorSession
|
||||
me.editorSession.destroy (e) ->
|
||||
return (me.error "Cannot destroy editor session " + e) if e
|
||||
me.editorSession = undefined
|
||||
# destroy session
|
||||
me.session.destroy (e) ->
|
||||
return (me.error "Cannot destroy document session " + e) if e
|
||||
core.Async.destroyAll [me.canvas.destroy], (e) ->
|
||||
return me.error "Cannot destroy canvas" + e if e
|
||||
me.notify "Document closed"
|
||||
me.session = undefined
|
||||
me.directFormattingCtl = undefined
|
||||
#
|
||||
|
||||
|
||||
cleanup: (e) ->
|
||||
@closeDocument()
|
||||
|
||||
this.OS.register "OpenPage", OpenPage
|
Reference in New Issue
Block a user