forked from: ContextMenu

by 9re forked from ContextMenu (diff: 29)
The best solution for 'Copy to Clipboard' would be...

1. set contextMenu.clipboardMenu to true
2. set contextMenu.clipboradItems.copy to true
3. then, if the user clicks the 'Copy' in the ContextMenu, Event.COPY will be dispatched
4. handle the Event.COPY event. you can copy to clipboard in this event handler.


=> Sorry, this one neither supports Mac Safari... Not the best way.
♥0 | Line 56 | Modified 2010-10-28 11:15:35 | MIT License
play

ActionScript3 source code

/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/bSAi
 */

// forked from 9re's ContextMenu
package {
    import flash.events.Event;
    import flash.ui.ContextMenuClipboardItems;
    import flash.utils.setTimeout;
    import flash.events.MouseEvent;
    import flash.text.engine.TextLine;
    import flash.text.engine.ElementFormat;
    import flash.text.engine.TextElement;
    import flash.text.engine.TextBlock;
    import flash.text.TextFormat;
    import flash.events.ContextMenuEvent;
    import flash.ui.ContextMenuItem;
    import flash.ui.ContextMenu;
    import flash.desktop.ClipboardFormats;
    import flash.desktop.Clipboard;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            var factory:TextBlock = new TextBlock;
            var errorFormat:ElementFormat = new ElementFormat;
            var normalFormat:ElementFormat;
            normalFormat = errorFormat.clone();
            errorFormat.color = 0xff0000;
            var yPos:int = 16;
            
            graphics.beginFill(0xffffff);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            
            var _this:FlashTest = this;
            addEventListener(Event.COPY, function ():void {
                try {
                    printLine("trying to copy to clipboard...");
                    Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, "copy test");
                } catch (e:Error) {
                    printLine("copying failed : " + e, errorFormat);
                    return;
                }
                printLine("copy succeeded!");
            });
            
            var str:String;
            contextMenu = new ContextMenu();
            contextMenu.hideBuiltInItems();
            contextMenu.clipboardMenu = true;
            contextMenu.clipboardItems.copy = true;
            
            function printLine(str:String, format:ElementFormat = null):void {
                format ||= normalFormat;
                factory.content = new TextElement(str, format);
                var line:TextLine = null;
                while (line = factory.createTextLine(line, 465)) {
                    line.y = yPos;
                    yPos += 16;
                    addChild(line);
                }
            }
            
        }
    }
}

Forked